-
Notifications
You must be signed in to change notification settings - Fork 37
disable grc in global hub. #450
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
openshift-merge-robot
merged 4 commits into
stolostron:main
from
morvencao:br_disable_grc_in_global_hub
May 25, 2023
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package hubofhubs | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| mchv1 "github.com/stolostron/multiclusterhub-operator/api/v1" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/apimachinery/pkg/api/meta" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
|
|
||
| operatorv1alpha2 "github.com/stolostron/multicluster-global-hub/operator/apis/v1alpha2" | ||
| "github.com/stolostron/multicluster-global-hub/operator/pkg/condition" | ||
| ) | ||
|
|
||
| func (r *MulticlusterGlobalHubReconciler) reconcileMCH(ctx context.Context, | ||
| mgh *operatorv1alpha2.MulticlusterGlobalHub, | ||
| ) error { | ||
| mch, _ := getMCH(ctx, r.Client) | ||
| // skip reconcile mch if it is not found | ||
| if mch == nil { | ||
| return nil | ||
| } | ||
|
|
||
| mch, _ = disableGRCInMCH(mch) | ||
|
|
||
| // if grcDisabledByMGH { | ||
| // // set the annotation to remember grc status | ||
| // annotations := mgh.GetAnnotations() | ||
| // annotations[GRCDisabledByMGHAnnotation] = strconv.FormatBool(grcDisabledByMGH) | ||
| // mgh.SetAnnotations(annotations) | ||
| // if err := r.Client.Update(ctx, mgh, &client.UpdateOptions{}); err != nil { | ||
| // return fmt.Errorf("failed to annotation(%s) to mgh. err = %v", GRCDisabledByMGHAnnotation, err) | ||
| // } | ||
| // } | ||
|
|
||
| if err := r.Client.Update(ctx, mch); err != nil { | ||
| return fmt.Errorf("failed to update MCH instance. err = %v", err) | ||
| } | ||
|
|
||
| if err := condition.SetConditionGRCDisabled(ctx, r.Client, mgh, | ||
| condition.CONDITION_STATUS_TRUE); err != nil { | ||
| return condition.FailToSetConditionError(condition.CONDITION_STATUS_TRUE, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func disableGRCInMCH(mch *mchv1.MultiClusterHub) (*mchv1.MultiClusterHub, bool) { | ||
| grcDisabledByMGH := false | ||
| if mch.Spec.Overrides != nil { | ||
| found := false | ||
| for i, c := range mch.Spec.Overrides.Components { | ||
| if c.Name == "grc" { | ||
| if c.Enabled { | ||
| mch.Spec.Overrides.Components[i].Enabled = false | ||
| grcDisabledByMGH = true | ||
| } | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| if !found { | ||
| mch.Spec.Overrides.Components = append(mch.Spec.Overrides.Components, | ||
| mchv1.ComponentConfig{ | ||
| Name: "grc", | ||
| Enabled: false, | ||
| }) | ||
| grcDisabledByMGH = true | ||
| } | ||
| } else { | ||
| mch.Spec.Overrides = &mchv1.Overrides{ | ||
| Components: []mchv1.ComponentConfig{ | ||
| { | ||
| Name: "grc", | ||
| Enabled: false, | ||
| }, | ||
| }, | ||
| } | ||
| grcDisabledByMGH = true | ||
| } | ||
|
|
||
| return mch, grcDisabledByMGH | ||
| } | ||
|
|
||
| func getMCH(ctx context.Context, k8sClient client.Client) (*mchv1.MultiClusterHub, error) { | ||
| mch := &mchv1.MultiClusterHubList{} | ||
| err := k8sClient.List(ctx, mch) | ||
| if errors.IsNotFound(err) { | ||
| return nil, nil | ||
| } | ||
| if meta.IsNoMatchError(err) { | ||
| return nil, nil | ||
| } | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if len(mch.Items) == 0 { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &mch.Items[0], nil | ||
| } | ||
110 changes: 110 additions & 0 deletions
110
operator/pkg/controllers/hubofhubs/globalhub_mch_test.go
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,110 @@ | ||
| package hubofhubs | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| mchv1 "github.com/stolostron/multiclusterhub-operator/api/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| "github.com/stolostron/multicluster-global-hub/operator/pkg/config" | ||
| ) | ||
|
|
||
| func TestDisableGRCInMCH(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| mch *mchv1.MultiClusterHub | ||
| grcDisabled bool | ||
| }{ | ||
| { | ||
| name: "no need to disable grc", | ||
| mch: &mchv1.MultiClusterHub{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "multiclusterhub", | ||
| Namespace: config.GetDefaultNamespace(), | ||
| }, | ||
| Spec: mchv1.MultiClusterHubSpec{ | ||
| Overrides: &mchv1.Overrides{ | ||
| Components: []mchv1.ComponentConfig{ | ||
| { | ||
| Name: "grc", | ||
| Enabled: false, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| grcDisabled: false, | ||
| }, | ||
| { | ||
| name: "disable grc explicitly", | ||
| mch: &mchv1.MultiClusterHub{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "multiclusterhub", | ||
| Namespace: config.GetDefaultNamespace(), | ||
| }, | ||
| Spec: mchv1.MultiClusterHubSpec{ | ||
| Overrides: &mchv1.Overrides{ | ||
| Components: []mchv1.ComponentConfig{ | ||
| { | ||
| Name: "grc", | ||
| Enabled: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| grcDisabled: true, | ||
| }, | ||
| { | ||
| name: "disable grc by adding component explicitly", | ||
| mch: &mchv1.MultiClusterHub{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "multiclusterhub", | ||
| Namespace: config.GetDefaultNamespace(), | ||
| }, | ||
| Spec: mchv1.MultiClusterHubSpec{ | ||
| Overrides: &mchv1.Overrides{ | ||
| Components: []mchv1.ComponentConfig{}, | ||
| }, | ||
| }, | ||
| }, | ||
| grcDisabled: true, | ||
| }, | ||
| { | ||
| name: "disable grc by adding overrides explicitly", | ||
| mch: &mchv1.MultiClusterHub{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "multiclusterhub", | ||
| Namespace: config.GetDefaultNamespace(), | ||
| }, | ||
| Spec: mchv1.MultiClusterHubSpec{}, | ||
| }, | ||
| grcDisabled: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| newMCH, getGRCDisabled := disableGRCInMCH(tc.mch) | ||
| if newMCH.Spec.Overrides == nil { | ||
| t.Errorf("empty MCH component overrides") | ||
| } | ||
| foundGRC := false | ||
| for _, c := range newMCH.Spec.Overrides.Components { | ||
| if c.Name == "grc" { | ||
| foundGRC = true | ||
| if c.Enabled == true { | ||
| t.Errorf("GRC is not disabled in MCH") | ||
| } | ||
| break | ||
| } | ||
| } | ||
| if !foundGRC { | ||
| t.Errorf("GRC is not add to MCH component overrides") | ||
| } | ||
| if getGRCDisabled != tc.grcDisabled { | ||
| t.Errorf("unexpected grcDisabled, got: %v, want: %v", getGRCDisabled, tc.grcDisabled) | ||
| } | ||
| }) | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There has a function to getMCH has been implemented before. Maybe we can just keep one of these to the
pkg.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they are not exactly the same.