Skip to content

Commit

Permalink
Removed Change annotation; Added TODO
Browse files Browse the repository at this point in the history
  • Loading branch information
tgarg-splunk committed Jul 25, 2023
1 parent 95423f8 commit ffeafdd
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 134 deletions.
4 changes: 3 additions & 1 deletion pkg/splunk/enterprise/monitoringconsole.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ func ApplyMonitoringConsole(ctx context.Context, client splcommon.ControllerClie
finalResult := handleAppFrameworkActivity(ctx, client, cr, &cr.Status.AppContext, &cr.Spec.AppFrameworkConfig)
result = *finalResult

// trigger SearchHeadCluster reconcile by changing the splunk/image-tag annotation
// TODO: Fix the Change Annotation logic/ find an alternative (eg. state machine); right now the search head deployer
// starts terminating with this and few replicas do not come up properly after that

// err = changeSearchHeadAnnotations(ctx, client, cr)
// if err != nil {
// return result, err
Expand Down
53 changes: 0 additions & 53 deletions pkg/splunk/enterprise/searchheadcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/remotecommand"
"sigs.k8s.io/controller-runtime/pkg/client"
rclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
Expand Down Expand Up @@ -735,55 +734,3 @@ func isSearchHeadReadyForUpgrade(ctx context.Context, c splcommon.ControllerClie

return true, nil
}

// changeSearchHeadAnnotations updates the splunk/image-tag field of the SearchHeadCluster annotations to trigger the reconcile loop
// on update, and returns error if something is wrong.
func changeSearchHeadAnnotations(ctx context.Context, client splcommon.ControllerClient, cr *enterpriseApi.MonitoringConsole) error {
reqLogger := log.FromContext(ctx)
scopedLog := reqLogger.WithName("changeSearchHeadAnnotations").WithValues("name", cr.GetName(), "namespace", cr.GetNamespace())
eventPublisher, _ := newK8EventPublisher(client, cr)

searchHeadClusterInstance := enterpriseApi.SearchHeadCluster{}

// List out all the SearchHeadCluster instances in the namespace
opts := []rclient.ListOption{
rclient.InNamespace(cr.GetNamespace()),
}
objectList := enterpriseApi.SearchHeadClusterList{}
err := client.List(ctx, &objectList, opts...)
if err != nil {
if err.Error() == "NotFound" {
return nil
}
return err
}
if len(objectList.Items) == 0 {
return nil
}

// check if instance has the required MonitoringConsoleRef
for _, shc := range objectList.Items {
if shc.Spec.MonitoringConsoleRef.Name == cr.GetName() {
searchHeadClusterInstance = shc
}
}
if len(searchHeadClusterInstance.GetName()) == 0 {
return nil
}

image, err := getCurrentImage(ctx, client, cr, SplunkMonitoringConsole)
if err != nil {
eventPublisher.Warning(ctx, "changeSearchHeadAnnotations", fmt.Sprintf("Could not get the MonitoringConsole Image. Reason %v", err))
scopedLog.Error(err, "Get MonitoringConsole Image failed with", "error", err)
return err
}

err = changeAnnotations(ctx, client, image, &searchHeadClusterInstance)
if err != nil {
eventPublisher.Warning(ctx, "changeSearchHeadAnnotations", fmt.Sprintf("Could not update annotations. Reason %v", err))
scopedLog.Error(err, "SearchHeadCluster types update after changing annotations failed with", "error", err)
return err
}

return nil
}
80 changes: 0 additions & 80 deletions pkg/splunk/enterprise/searchheadcluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1961,83 +1961,3 @@ func TestIsSearchHeadReadyForUpgrade(t *testing.T) {
t.Errorf("isSearchHeadReadyForUpgrade: SHC should be ready for upgrade")
}
}

func TestChangeSearchHeadAnnotations(t *testing.T) {
ctx := context.TODO()

// define MC and SHC
mc := &enterpriseApi.MonitoringConsole{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
Spec: enterpriseApi.MonitoringConsoleSpec{
CommonSplunkSpec: enterpriseApi.CommonSplunkSpec{
Spec: enterpriseApi.Spec{
ImagePullPolicy: "Always",
},
Volumes: []corev1.Volume{},
},
},
}

shc := &enterpriseApi.SearchHeadCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
Spec: enterpriseApi.SearchHeadClusterSpec{
CommonSplunkSpec: enterpriseApi.CommonSplunkSpec{
Spec: enterpriseApi.Spec{
ImagePullPolicy: "Always",
},
Volumes: []corev1.Volume{},
MonitoringConsoleRef: corev1.ObjectReference{
Name: "test",
},
},
},
}
mc.Spec.Image = "splunk/splunk:latest"

builder := fake.NewClientBuilder()
client := builder.Build()
utilruntime.Must(enterpriseApi.AddToScheme(clientgoscheme.Scheme))

// Create the instances
client.Create(ctx, mc)
_, err := ApplyMonitoringConsole(ctx, client, mc)
if err != nil {
t.Errorf("applyMonitoringConsole should not have returned error; err=%v", err)
}
mc.Status.Phase = enterpriseApi.PhaseReady
err = client.Status().Update(ctx, mc)
if err != nil {
t.Errorf("Unexpected update pod %v", err)
debug.PrintStack()
}
client.Create(ctx, shc)
_, err = ApplySearchHeadCluster(ctx, client, shc)
if err != nil {
t.Errorf("applySearchHeadCluster should not have returned error; err=%v", err)
}

err = changeSearchHeadAnnotations(ctx, client, mc)
if err != nil {
t.Errorf("changeSearchHeadAnnotations should not have returned error=%v", err)
}
searchHeadCluster := &enterpriseApi.SearchHeadCluster{}
namespacedName := types.NamespacedName{
Name: shc.Name,
Namespace: shc.Namespace,
}
err = client.Get(ctx, namespacedName, searchHeadCluster)
if err != nil {
t.Errorf("changeSearchHeadAnnotations should not have returned error=%v", err)
}

annotations := searchHeadCluster.GetAnnotations()
if annotations["splunk/image-tag"] != mc.Spec.Image {
t.Errorf("changeSearchHeadAnnotations should have set the splunk/image-tag annotation field to the current image")
}
}

0 comments on commit ffeafdd

Please sign in to comment.