-
Notifications
You must be signed in to change notification settings - Fork 123
CSPL-720: Adding support to configure a custom serviceaccount per CRD #220
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9aa689b
Adding support to configure a custom serviceaccount per CRD
akondur 679498f
Fix merge conflicts
akondur c41e0f5
Additional ut check for serviceaccount
akondur 90b391b
Additional ut for update scenario
akondur afba86e
Addressing review comments
akondur f347b90
Give access to the resource serviceaccount
akondur 9cc89e1
Changed logging for GetServiceAccount
akondur 5a97236
Merge branch 'develop' into custom_service_account
akondur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ rules: | |
- secrets | ||
- pods | ||
- pods/exec | ||
- serviceaccounts | ||
verbs: | ||
- create | ||
- delete | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright (c) 2018-2020 Splunk Inc. All rights reserved. | ||
akondur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package controller | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
splcommon "github.com/splunk/splunk-operator/pkg/splunk/common" | ||
splutil "github.com/splunk/splunk-operator/pkg/splunk/util" | ||
) | ||
|
||
// ApplyServiceAccount creates or updates a Kubernetes serviceAccount | ||
func ApplyServiceAccount(client splcommon.ControllerClient, serviceAccount *corev1.ServiceAccount) error { | ||
scopedLog := log.WithName("ApplyServiceAccount").WithValues("serviceAccount", serviceAccount.GetName(), | ||
"namespace", serviceAccount.GetNamespace()) | ||
|
||
namespacedName := types.NamespacedName{Namespace: serviceAccount.GetNamespace(), Name: serviceAccount.GetName()} | ||
var current corev1.ServiceAccount | ||
|
||
err := client.Get(context.TODO(), namespacedName, ¤t) | ||
if err == nil { | ||
if !reflect.DeepEqual(serviceAccount, ¤t) { | ||
akondur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
scopedLog.Info("Updating service account") | ||
current = *serviceAccount | ||
err = splutil.UpdateResource(client, ¤t) | ||
} | ||
} else { | ||
err = splutil.CreateResource(client, serviceAccount) | ||
} | ||
|
||
return err | ||
} | ||
|
||
// GetServiceAccount gets the serviceAccount resource in a given namespace | ||
func GetServiceAccount(client splcommon.ControllerClient, namespacedName types.NamespacedName) (*corev1.ServiceAccount, error) { | ||
var serviceAccount corev1.ServiceAccount | ||
err := client.Get(context.TODO(), namespacedName, &serviceAccount) | ||
if err != nil { | ||
scopedLog := log.WithName("GetServiceAccount").WithValues("serviceAccount", namespacedName.Name, | ||
"namespace", namespacedName.Namespace, "error", err) | ||
scopedLog.Info("ServiceAccount not found") | ||
return nil, err | ||
} | ||
return &serviceAccount, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright (c) 2018-2020 Splunk Inc. All rights reserved. | ||
akondur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package controller | ||
|
||
import ( | ||
"testing" | ||
|
||
spltest "github.com/splunk/splunk-operator/pkg/splunk/test" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
func TestApplyServiceAccount(t *testing.T) { | ||
funcCalls := []spltest.MockFuncCall{{MetaName: "*v1.ServiceAccount-test-defaults"}} | ||
createCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Create": funcCalls} | ||
updateCalls := map[string][]spltest.MockFuncCall{"Get": funcCalls, "Update": funcCalls} | ||
current := corev1.ServiceAccount{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "defaults", | ||
Namespace: "test", | ||
}, | ||
} | ||
revised := current.DeepCopy() | ||
revised.ResourceVersion = "dummy" | ||
reconcile := func(c *spltest.MockClient, cr interface{}) error { | ||
err := ApplyServiceAccount(c, cr.(*corev1.ServiceAccount)) | ||
return err | ||
} | ||
spltest.ReconcileTester(t, "TestApplyServiceAccount", ¤t, revised, createCalls, updateCalls, reconcile, false) | ||
} | ||
|
||
func TestGetServiceAccount(t *testing.T) { | ||
current := corev1.ServiceAccount{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "defaults", | ||
Namespace: "test", | ||
}, | ||
} | ||
|
||
client := spltest.NewMockClient() | ||
namespacedName := types.NamespacedName{Namespace: current.GetNamespace(), Name: current.GetName()} | ||
|
||
// serviceAccount doesn't exist | ||
_, err := GetServiceAccount(client, namespacedName) | ||
if err == nil { | ||
t.Errorf("Should return an error, when the serviceAccount doesn't exist") | ||
} | ||
|
||
// Create serviceAccount | ||
err = ApplyServiceAccount(client, ¤t) | ||
if err != nil { | ||
t.Errorf("Failed to create the serviceAccount. Error: %s", err.Error()) | ||
} | ||
|
||
// Make sure serviceAccount exists | ||
got, err := GetServiceAccount(client, namespacedName) | ||
if err != nil { | ||
if got.GetName() != current.GetName() { | ||
t.Errorf("Incorrect service account retrieved got %s want %s", got.GetName(), current.GetName()) | ||
} | ||
t.Errorf("Should not return an error, when the serviceAccount exists") | ||
} | ||
|
||
var dummySaName string = "dummy_sa" | ||
|
||
current.Name = dummySaName | ||
// Update serviceAccount | ||
err = ApplyServiceAccount(client, ¤t) | ||
if err != nil { | ||
t.Errorf("Failed to create the serviceAccount. Error: %s", err.Error()) | ||
} | ||
|
||
// Make sure serviceAccount is updated | ||
got, err = GetServiceAccount(client, namespacedName) | ||
if err != nil { | ||
if got.GetName() != dummySaName { | ||
t.Errorf("Incorrect service account retrieved got %s want %s", got.GetName(), current.GetName()) | ||
} | ||
t.Errorf("Should not return an error, when the serviceAccount exists") | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.