Skip to content

Commit

Permalink
Ensure map initialized before accessing it, create secret only if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok committed Dec 23, 2021
1 parent 21a4f72 commit 42ff260
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
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

0 comments on commit 42ff260

Please sign in to comment.