Skip to content
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

Add watch logic for addondeploymentconfig #1287

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -325,31 +325,6 @@ func createManifestWorks(
}
}

// If ProxyConfig is specified as part of addonConfig, set the proxy envs
if clusterName != localClusterName {
for i := range spec.Containers {
container := &spec.Containers[i]
if addonConfig.Spec.ProxyConfig.HTTPProxy != "" {
container.Env = append(container.Env, corev1.EnvVar{
Name: "HTTP_PROXY",
Value: addonConfig.Spec.ProxyConfig.HTTPProxy,
})
}
if addonConfig.Spec.ProxyConfig.HTTPSProxy != "" {
container.Env = append(container.Env, corev1.EnvVar{
Name: "HTTPS_PROXY",
Value: addonConfig.Spec.ProxyConfig.HTTPSProxy,
})
}
if addonConfig.Spec.ProxyConfig.NoProxy != "" {
container.Env = append(container.Env, corev1.EnvVar{
Name: "NO_PROXY",
Value: addonConfig.Spec.ProxyConfig.NoProxy,
})
}
}
}

if hasCustomRegistry {
oldImage := container.Image
newImage, err := imageRegistryClient.Cluster(clusterName).ImageOverride(oldImage)
Expand All @@ -360,6 +335,32 @@ func createManifestWorks(
}
}
}
for i := range spec.Containers {
if spec.Containers[i].Name == "endpoint-observability-operator" {
container := &spec.Containers[i]

if clusterName != localClusterName {
if addonConfig.Spec.ProxyConfig.HTTPProxy != "" {
container.Env = append(container.Env, corev1.EnvVar{
Name: "HTTP_PROXY",
Value: addonConfig.Spec.ProxyConfig.HTTPProxy,
})
}
if addonConfig.Spec.ProxyConfig.HTTPSProxy != "" {
container.Env = append(container.Env, corev1.EnvVar{
Name: "HTTPS_PROXY",
Value: addonConfig.Spec.ProxyConfig.HTTPSProxy,
})
}
if addonConfig.Spec.ProxyConfig.NoProxy != "" {
container.Env = append(container.Env, corev1.EnvVar{
Name: "NO_PROXY",
Value: addonConfig.Spec.ProxyConfig.NoProxy,
})
}
}
}
}
log.Info(fmt.Sprintf("Cluster: %+v, Spec.NodeSelector (after): %+v", clusterName, spec.NodeSelector))
log.Info(fmt.Sprintf("Cluster: %+v, Spec.Tolerations (after): %+v", clusterName, spec.Tolerations))
dep.Spec.Template.Spec = spec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,34 @@ func (r *PlacementRuleReconciler) SetupWithManager(mgr ctrl.Manager) error {

clusterPred := getClusterPreds()

// Watch changes for AddonDeploymentConfig
AddonDeploymentPred := predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
//if e.Object.GetName() == defaultAddonDeploymentConfig.Name &&
// e.Object.GetNamespace() == defaultAddonDeploymentConfig.Namespace {
// log.Info("default AddonDeploymentConfig is created")
// return true
//}
return true
},
UpdateFunc: func(e event.UpdateEvent) bool {
if e.ObjectNew.GetName() == defaultAddonDeploymentConfig.Name &&
e.ObjectNew.GetNamespace() == defaultAddonDeploymentConfig.Namespace {
log.Info("default AddonDeploymentConfig is updated")
return true
}
return false
},
DeleteFunc: func(e event.DeleteEvent) bool {
if e.Object.GetName() == defaultAddonDeploymentConfig.Name &&
e.Object.GetNamespace() == defaultAddonDeploymentConfig.Namespace {
log.Info("default AddonDeploymentConfig is deleted")
return true
}
return false
},
}

obsAddonPred := predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return false
Expand Down Expand Up @@ -829,6 +857,20 @@ func (r *PlacementRuleReconciler) SetupWithManager(mgr ctrl.Manager) error {
// secondary watch for alertmanager accessor serviceaccount
Watches(&source.Kind{Type: &corev1.ServiceAccount{}}, &handler.EnqueueRequestForObject{}, builder.WithPredicates(amAccessorSAPred))

// watch for AddonDeploymentConfig
if _, err := r.RESTMapper.RESTMapping(schema.GroupKind{Group: addonv1alpha1.GroupVersion.Group, Kind: "AddOnDeploymentConfig"}, addonv1alpha1.GroupVersion.Version); err == nil {
ctrBuilder = ctrBuilder.Watches(
&source.Kind{Type: &addonv1alpha1.AddOnDeploymentConfig{}},
handler.EnqueueRequestsFromMapFunc(func(obj client.Object) []reconcile.Request {
return []reconcile.Request{
{NamespacedName: types.NamespacedName{
Name: config.AddonDeploymentConfigUpdateName,
}},
}
}),
builder.WithPredicates(AddonDeploymentPred),
)
}
manifestWorkGroupKind := schema.GroupKind{Group: workv1.GroupVersion.Group, Kind: "ManifestWork"}
if _, err := r.RESTMapper.RESTMapping(manifestWorkGroupKind, workv1.GroupVersion.Version); err == nil {
workPred := getManifestworkPred()
Expand Down Expand Up @@ -937,7 +979,8 @@ func StartPlacementController(mgr manager.Manager, crdMap map[string]bool) error
func isReconcileRequired(request ctrl.Request, managedCluster string) bool {
if request.Name == config.MCOUpdatedRequestName ||
request.Name == config.MCHUpdatedRequestName ||
request.Name == config.ClusterManagementAddOnUpdateName {
request.Name == config.ClusterManagementAddOnUpdateName ||
request.Name == config.AddonDeploymentConfigUpdateName {
return true
}
if request.Namespace == config.GetDefaultNamespace() ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strings"
"testing"

appsv1 "k8s.io/api/apps/v1"

ocinfrav1 "github.com/openshift/api/config/v1"
operatorv1 "github.com/openshift/api/operator/v1"
routev1 "github.com/openshift/api/route/v1"
Expand Down Expand Up @@ -177,7 +179,7 @@ func TestObservabilityAddonController(t *testing.T) {
},
}
objs := []runtime.Object{mco, pull, newConsoleRoute(), newTestObsApiRoute(), newTestAlertmanagerRoute(), newTestIngressController(), newTestRouteCASecret(), newCASecret(), newCertSecret(mcoNamespace), NewMetricsAllowListCM(),
NewAmAccessorSA(), NewAmAccessorTokenSecret(), newManagedClusterAddon(), deprecatedRole, newClusterMgmtAddon(),
NewAmAccessorSA(), NewAmAccessorTokenSecret(), deprecatedRole, newClusterMgmtAddon(),
newAddonDeploymentConfig(defaultAddonConfigName, namespace), newAddonDeploymentConfig(addonConfigName, namespace)}
c := fake.NewClientBuilder().WithRuntimeObjects(objs...).Build()
r := &PlacementRuleReconciler{Client: c, Scheme: s, CRDMap: map[string]bool{config.IngressControllerCRD: true}}
Expand Down Expand Up @@ -234,6 +236,69 @@ func TestObservabilityAddonController(t *testing.T) {
t.Fatalf("reconcile: (%v)", err)
}

foundAddonDeploymentConfig := &addonv1alpha1.AddOnDeploymentConfig{}
err = c.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: defaultAddonConfigName}, foundAddonDeploymentConfig)
if err != nil {
t.Fatalf("Failed to get addondeploymentconfig %s: (%v)", name, err)
}

//Change proxyconfig in addondeploymentconfig
foundAddonDeploymentConfig.Spec.ProxyConfig = addonv1alpha1.ProxyConfig{
HTTPProxy: "http://test1.com",
HTTPSProxy: "https://test1.com",
NoProxy: "test.com",
}

err = c.Update(context.TODO(), foundAddonDeploymentConfig)
if err != nil {
t.Fatalf("Failed to update addondeploymentconfig %s: (%v)", name, err)
}

req = ctrl.Request{
NamespacedName: types.NamespacedName{
Name: config.AddonDeploymentConfigUpdateName,
},
}

_, err = r.Reconcile(context.TODO(), req)
if err != nil {
t.Fatalf("reconcile after updating addondeploymentconfig: (%v)", err)
}

foundManifestwork := &workv1.ManifestWork{}
err = c.Get(context.TODO(), types.NamespacedName{Name: namespace + workNameSuffix, Namespace: namespace}, foundManifestwork)
if err != nil {
t.Fatalf("Failed to get manifestwork %s: (%v)", namespace, err)
}
for _, manifest := range foundManifestwork.Spec.Workload.Manifests {
obj, _ := util.GetObject(manifest.RawExtension)
if obj.GetObjectKind().GroupVersionKind().Kind == "Deployment" {
//Check the proxy env variables
deployment := obj.(*appsv1.Deployment)
spec := deployment.Spec.Template.Spec
for _, c := range spec.Containers {
if c.Name == "endpoint-observability-operator" {
env := c.Env
for _, e := range env {
if e.Name == "HTTP_PROXY" {
if e.Value != "http://test1.com" {
t.Fatalf("HTTP_PROXY is not set correctly: expected %s, got %s", "http://test1.com", e.Value)
}
} else if e.Name == "HTTPS_PROXY" {
if e.Value != "https://test1.com" {
t.Fatalf("HTTPS_PROXY is not set correctly: expected %s, got %s", "https://test1.com", e.Value)
}
} else if e.Name == "NO_PROXY" {
if e.Value != "test.com" {
t.Fatalf("NO_PROXY is not set correctly: expected %s, got %s", "test.com", e.Value)
}
}
}
}
}
}
}

err = c.Delete(context.TODO(), mco)
if err != nil {
t.Fatalf("Failed to delete mco: (%v)", err)
Expand Down Expand Up @@ -310,7 +375,7 @@ func TestObservabilityAddonController(t *testing.T) {
// test mco-disable-alerting annotation
// 1. Verify that alertmanager-endpoint in secret hub-info-secret in the ManifestWork is not null
t.Logf("check alertmanager endpoint is not null")
foundManifestwork := &workv1.ManifestWork{}
foundManifestwork = &workv1.ManifestWork{}
err = c.Get(context.TODO(), types.NamespacedName{Name: namespace + workNameSuffix, Namespace: namespace}, foundManifestwork)
if err != nil {
t.Fatalf("Failed to get manifestwork %s: (%v)", namespace, err)
Expand Down Expand Up @@ -553,6 +618,11 @@ func newAddonDeploymentConfig(name, namespace string) *addonv1alpha1.AddOnDeploy
"kubernetes.io/os": "linux",
},
},
ProxyConfig: addonv1alpha1.ProxyConfig{
HTTPProxy: "http://foo.com",
HTTPSProxy: "https://foo.com",
NoProxy: "bar.com",
},
},
}
}
1 change: 1 addition & 0 deletions operators/multiclusterobservability/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const (
MCHUpdatedRequestName = "mch-updated-request"
MCOUpdatedRequestName = "mco-updated-request"
ClusterManagementAddOnUpdateName = "clustermgmtaddon-updated-request"
AddonDeploymentConfigUpdateName = "addondc-updated-request"
MulticloudConsoleRouteName = "multicloud-console"
ImageManifestConfigMapNamePrefix = "mch-image-manifest-"
OCMManifestConfigMapTypeLabelKey = "ocm-configmap-type"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func CreateClusterManagementAddon(c client.Client) (
found := &addonv1alpha1.ClusterManagementAddOn{}
err = c.Get(context.TODO(), types.NamespacedName{Name: ObservabilityController}, found)
if err != nil && errors.IsNotFound(err) {

if err := c.Create(context.TODO(), clusterManagementAddon); err != nil {
log.Error(err, "Failed to create observability-controller clustermanagementaddon ")
return nil, err
Expand Down
1 change: 1 addition & 0 deletions operators/multiclusterobservability/tests/manifests
Loading