Skip to content

Commit

Permalink
[INSTA-24606] Change deployment and rbac naming (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
Milica-Cvrkota-IBM authored Jan 30, 2025
1 parent eceb1ce commit 4e73445
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ setup: ## Basic project setup, e.g. installing GitHook for checking license head
cd .git/hooks && ln -fs ../../.githooks/* .

manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=instana-agent-clusterrole webhook paths="./..." output:crd:artifacts:config=config/crd/bases

generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
creationTimestamp: null
name: manager-role
name: instana-agent-clusterrole
rules:
- apiGroups:
- agents.instana.io
Expand Down
2 changes: 1 addition & 1 deletion config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ metadata:
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
name: instana-agent-controller-manager
namespace: system
labels:
app.kubernetes.io/name: instana-agent-operator
Expand Down
2 changes: 1 addition & 1 deletion config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: manager-role
name: instana-agent-clusterrole
rules:
- nonResourceURLs:
- /healthz
Expand Down
4 changes: 2 additions & 2 deletions config/rbac/role_binding.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: manager-rolebinding
name: instana-agent-clusterrolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: manager-role
name: instana-agent-clusterrole
subjects:
- kind: ServiceAccount
name: instana-agent-operator
Expand Down
67 changes: 63 additions & 4 deletions e2e/agent_test_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
v1 "github.com/instana/instana-agent-operator/api/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -75,7 +77,7 @@ func EnsureAgentNamespaceDeletion() env.Func {
}

// full purge of resources if anything would be left in the cluster
p = utils.RunCommand("kubectl delete crd/agents.instana.io clusterrole/instana-agent-k8sensor clusterrole/manager-role clusterrole/leader-election-role clusterrolebinding/leader-election-rolebinding clusterrolebinding/manager-rolebinding")
p = utils.RunCommand("kubectl delete crd/agents.instana.io clusterrole/instana-agent-k8sensor clusterrole/instana-agent-clusterrole clusterrole/leader-election-role clusterrolebinding/leader-election-rolebinding clusterrolebinding/instana-agent-clusterrolebinding")
if p.Err() != nil {
log.Warningf("Could not remove some artifacts, ignoring as they might not be present %s - %s - %s - %d", p.Command(), p.Err(), p.Out(), p.ExitCode())
}
Expand Down Expand Up @@ -317,7 +319,7 @@ func SetupOperatorDevBuild() e2etypes.StepFunc {

func DeployAgentCr(agent *v1.InstanaAgent) e2etypes.StepFunc {
return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
// Wait for controller-manager deployment to ensure that CRD is installed correctly before proceeding.
// Wait for instana-agent-controller-manager deployment to ensure that CRD is installed correctly before proceeding.
// Technically, it could be categorized as "Assess" method, but the setup process requires to wait in between.
// Therefore, keeping the wait logic in this section.
client, err := cfg.NewClient()
Expand Down Expand Up @@ -389,6 +391,63 @@ func WaitForAgentDaemonSetToBecomeReady(args ...string) e2etypes.StepFunc {
}
}

func EnsureOldControllerManagerDeploymentIsNotRunning() e2etypes.StepFunc {
return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
t.Logf("Ensuring the old deployment %s is not running", InstanaOperatorOldDeploymentName)
client, err := cfg.NewClient()
if err != nil {
t.Fatal(err)
}
dep := appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: InstanaOperatorOldDeploymentName, Namespace: cfg.Namespace()},
}
err = wait.For(conditions.New(client.Resources()).ResourceDeleted(&dep), wait.WithTimeout(time.Minute*2))
if err != nil {
t.Fatal(err)
}
t.Logf("Deployment %s is deleted", InstanaOperatorOldDeploymentName)
return ctx
}
}

func EnsureOldClusterRoleIsGone() e2etypes.StepFunc {
return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
t.Logf("Ensuring the old clusterrole %s is not running", InstanaOperatorOldClusterRoleName)
client, err := cfg.NewClient()
if err != nil {
t.Fatal(err)
}
clusterrole := rbacv1.ClusterRole{
ObjectMeta: metav1.ObjectMeta{Name: InstanaOperatorOldClusterRoleName},
}
err = wait.For(conditions.New(client.Resources()).ResourceDeleted(&clusterrole), wait.WithTimeout(time.Minute*2))
if err != nil {
t.Fatal(err)
}
t.Logf("ClusteRole %s is deleted", InstanaOperatorOldClusterRoleName)
return ctx
}
}

func EnsureOldClusterRoleBindingIsGone() e2etypes.StepFunc {
return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
t.Logf("Ensuring the old clusterrolebinding %s is not running", InstanaOperatorOldClusterRoleBindingName)
client, err := cfg.NewClient()
if err != nil {
t.Fatal(err)
}
clusterrolebinding := rbacv1.ClusterRoleBinding{
ObjectMeta: metav1.ObjectMeta{Name: InstanaOperatorOldClusterRoleBindingName},
}
err = wait.For(conditions.New(client.Resources()).ResourceDeleted(&clusterrolebinding), wait.WithTimeout(time.Minute*2))
if err != nil {
t.Fatal(err)
}
t.Logf("ClusteRoleBinding %s is deleted", InstanaOperatorOldClusterRoleBindingName)
return ctx
}
}

func WaitForAgentSuccessfulBackendConnection() e2etypes.StepFunc {
return func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
t.Log("Searching for successful backend connection in agent logs")
Expand All @@ -407,8 +466,8 @@ func WaitForAgentSuccessfulBackendConnection() e2etypes.StepFunc {
connectionSuccessful := false
var buf *bytes.Buffer
for i := 0; i < 9; i++ {
t.Log("Sleeping 10 seconds")
time.Sleep(10 * time.Second)
t.Log("Sleeping 20 seconds")
time.Sleep(20 * time.Second)
t.Log("Fetching logs")
logReq := clientSet.CoreV1().Pods(cfg.Namespace()).GetLogs(podList.Items[0].Name, &corev1.PodLogOptions{})
podLogs, err := logReq.Stream(ctx)
Expand Down
5 changes: 4 additions & 1 deletion e2e/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ type OperatorImage struct {
var InstanaTestCfg InstanaTestConfig

const InstanaNamespace string = "instana-agent"
const InstanaOperatorDeploymentName string = "controller-manager"
const InstanaOperatorOldDeploymentName string = "controller-manager"
const InstanaOperatorOldClusterRoleName string = "manager-role"
const InstanaOperatorOldClusterRoleBindingName string = "manager-rolebinding"
const InstanaOperatorDeploymentName string = "instana-agent-controller-manager"
const AgentDaemonSetName string = "instana-agent"
const AgentCustomResourceName string = "instana-agent"
const K8sensorDeploymentName string = "instana-agent-k8sensor"
Expand Down
6 changes: 3 additions & 3 deletions e2e/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestInitialInstall(t *testing.T) {
initialInstallFeature := features.New("initial install dev-operator-build").
Setup(SetupOperatorDevBuild()).
Setup(DeployAgentCr(&agent)).
Assess("wait for controller-manager deployment to become ready", WaitForDeploymentToBecomeReady(InstanaOperatorDeploymentName)).
Assess("wait for instana-agent-controller-manager deployment to become ready", WaitForDeploymentToBecomeReady(InstanaOperatorDeploymentName)).
Assess("wait for k8sensor deployment to become ready", WaitForDeploymentToBecomeReady(K8sensorDeploymentName)).
Assess("wait for agent daemonset to become ready", WaitForAgentDaemonSetToBecomeReady()).
Assess("check agent log for successful connection", WaitForAgentSuccessfulBackendConnection()).
Expand All @@ -44,7 +44,7 @@ func TestUpdateInstall(t *testing.T) {
}
return ctx
}).
Setup(WaitForDeploymentToBecomeReady(InstanaOperatorDeploymentName)).
Setup(WaitForDeploymentToBecomeReady(InstanaOperatorOldDeploymentName)). //TODO: revert after the 2.1.15 release
Setup(DeployAgentCr(&agent)).
Assess("wait for k8sensor deployment to become ready", WaitForDeploymentToBecomeReady(K8sensorDeploymentName)).
Assess("wait for agent daemonset to become ready", WaitForAgentDaemonSetToBecomeReady()).
Expand All @@ -53,7 +53,7 @@ func TestUpdateInstall(t *testing.T) {

updateInstallDevBuildFeature := features.New("upgrade install from latest released to dev-operator-build").
Setup(SetupOperatorDevBuild()).
Assess("wait for controller-manager deployment to become ready", WaitForDeploymentToBecomeReady(InstanaOperatorDeploymentName)).
Assess("wait for instana-agent-controller-manager deployment to become ready", WaitForDeploymentToBecomeReady(InstanaOperatorDeploymentName)).
Assess("wait for k8sensor deployment to become ready", WaitForDeploymentToBecomeReady(K8sensorDeploymentName)).
Assess("wait for agent daemonset to become ready", WaitForAgentDaemonSetToBecomeReady()).
Assess("check agent log for successful connection", WaitForAgentSuccessfulBackendConnection()).
Expand Down
84 changes: 84 additions & 0 deletions e2e/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* (c) Copyright IBM Corp. 2025
* (c) Copyright Instana Inc. 2025
*/

package e2e

import (
"context"
"fmt"
"testing"

appsv1 "k8s.io/api/apps/v1"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"
"sigs.k8s.io/e2e-framework/support/utils"
)

func TestUpdateInstallFromOldGenericResourceNames(t *testing.T) {
agent := NewAgentCr(t)
installLatestFeature := features.New("deploy instana-agent-operator with the generic resource names (controller-manager, manager-role and manager-rolebinding)").
Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
const oldResourceNamesOperatorYamlUrl string = "https://github.com/instana/instana-agent-operator/releases/download/v2.1.14/instana-agent-operator.yaml"
t.Logf("Installing latest operator with the old, generic resource names from %s", oldResourceNamesOperatorYamlUrl)
p := utils.RunCommand(
fmt.Sprintf("kubectl apply -f %s", oldResourceNamesOperatorYamlUrl),
)
if p.Err() != nil {
t.Fatal("Error while applying the old operator yaml", p.Command(), p.Err(), p.Out(), p.ExitCode())
}
return ctx
}).
Setup(WaitForDeploymentToBecomeReady(InstanaOperatorOldDeploymentName)).
Setup(DeployAgentCr(&agent)).
Assess("wait for k8sensor deployment to become ready", WaitForDeploymentToBecomeReady(K8sensorDeploymentName)).
Assess("wait for agent daemonset to become ready", WaitForAgentDaemonSetToBecomeReady()).
Assess("check agent log for successful connection", WaitForAgentSuccessfulBackendConnection()).
Feature()

updateInstallDevBuildFeature := features.New("upgrade install from latest released to dev-operator-build").
Setup(SetupOperatorDevBuild()).
Assess("wait for instana-agent-controller-manager deployment to become ready", WaitForDeploymentToBecomeReady(InstanaOperatorDeploymentName)).
Assess("wait for k8sensor deployment to become ready", WaitForDeploymentToBecomeReady(K8sensorDeploymentName)).
Assess("wait for agent daemonset to become ready", WaitForAgentDaemonSetToBecomeReady()).
Assess("check agent log for successful connection", WaitForAgentSuccessfulBackendConnection()).
Feature()

checkReconciliationFeature := features.New("check reconcile works with new operator deployment").
Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
// delete agent daemonset
t.Log("Delete agent DaemonSet")
var ds appsv1.DaemonSet
if err := cfg.Client().Resources().Get(ctx, AgentDaemonSetName, cfg.Namespace(), &ds); err != nil {
t.Fatal(err)
}
if err := cfg.Client().Resources().Delete(ctx, &ds); err != nil {
t.Fatal(err)
}
t.Log("Agent DaemonSet deleted")

t.Log("Delete k8sensor Deployment")
var dep appsv1.Deployment
if err := cfg.Client().Resources().Get(ctx, K8sensorDeploymentName, cfg.Namespace(), &dep); err != nil {
t.Fatal(err)
}

if err := cfg.Client().Resources().Delete(ctx, &dep); err != nil {
t.Fatal(err)
}
t.Log("K8sensor Deployment deleted")
t.Log("Assessing reconciliation now")
return ctx
}).
Assess("confirm the old deployment is gone", EnsureOldControllerManagerDeploymentIsNotRunning()).
Assess("confirm the old clusterrole is gone", EnsureOldClusterRoleIsGone()).
Assess("confirm the old clusterrolebinding is gone", EnsureOldClusterRoleBindingIsGone()).
Assess("wait for k8sensor deployment to become ready", WaitForDeploymentToBecomeReady("instana-agent-k8sensor")).
Assess("wait for agent daemonset to become ready", WaitForAgentDaemonSetToBecomeReady()).
Assess("check agent log for successful connection", WaitForAgentSuccessfulBackendConnection()).
Feature()

// test feature
testEnv.Test(t, installLatestFeature, updateInstallDevBuildFeature, checkReconciliationFeature)
}
Loading

0 comments on commit 4e73445

Please sign in to comment.