Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure map initialized before accessing it, create secret only if needed #592

Merged
merged 1 commit into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/k8s-engine/controller/action_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,16 @@ func (a *ActionService) EnsureRunnerInputDataCreated(ctx context.Context, saName
return err
}

if !metav1.IsControlledBy(oldSecret, action) {
return errors.Errorf("Secret %q already exists and it is not owned by Action with the same name", key.String())
}

// Kubernetes allows creating Secret without data.
// We cannot be 100% sure that it was already initialized.
if oldSecret.Data == nil {
oldSecret.Data = map[string][]byte{}
}

oldSecret.Data[runnerContextSecretKey] = secret.Data[runnerContextSecretKey]
oldSecret.Data[runnerArgsSecretKey] = secret.Data[runnerArgsSecretKey]
return a.k8sCli.Update(ctx, oldSecret)
Expand Down
4 changes: 4 additions & 0 deletions internal/k8s-engine/graphql/domain/action/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ func (c *Converter) inputParamsFromGraphQL(in *graphql.ActionInputData, name str
data[ActionPolicySecretDataKey] = string(policyData)
}

if len(data) == 0 { // e.g. after unmarshaling we discovered that empty params were submitted
return nil, nil
}

return &v1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: secretKind,
Expand Down
3 changes: 3 additions & 0 deletions internal/k8s-engine/graphql/domain/action/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func TestConverter_FromGraphQLInput_HappyPath(t *testing.T) {
expectedModelPolicy: fixModelInputPolicy(name),
expectedModelSecret: fixModelInputSecret(name, false, true),
},
"Should ignore empty parameters and don't create secret": {
givenGQLParams: fixEmptyGQLInputParameters(),
},
}
for tn, tc := range tests {
t.Run(tn, func(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions internal/k8s-engine/graphql/domain/action/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ func fixGQLInputParameters() *graphql.JSON {
return &params
}

func fixEmptyGQLInputParameters() *graphql.JSON {
params := graphql.JSON(`{}`)
return &params
}

func fixGQLInputTypeInstances() []*graphql.InputTypeInstanceData {
return []*graphql.InputTypeInstanceData{
{
Expand Down
8 changes: 2 additions & 6 deletions internal/k8s-engine/graphql/domain/action/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,9 @@ func (s *Service) createInputParamsSecretIfShould(ctx context.Context, item mode
owner := item.Action
secret := item.InputParamsSecret
secret.SetResourceVersion("") // ResourceVersion can be not empty when using the Service directly

secret.SetOwnerReferences([]v1.OwnerReference{
{
APIVersion: v1alpha1.GroupVersion.Identifier(),
Kind: v1alpha1.ActionKind,
Name: owner.Name,
UID: owner.UID,
},
*metav1.NewControllerRef(&owner, v1alpha1.GroupVersion.WithKind(v1alpha1.ActionKind)),
})

log.Info("Creating Secret with input params")
Expand Down