Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
Signed-off-by: Coleen Iona Quadros <[email protected]>
  • Loading branch information
coleenquadros committed Dec 5, 2023
1 parent ff1e8c2 commit f2e9ad1
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,32 +325,6 @@ func createManifestWorks(
}
}

// If ProxyConfig is specified as part of addonConfig, set the proxy envs
//Coleen shouldnt this be out of th loop
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 @@ -361,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 @@ -542,12 +542,12 @@ func (r *PlacementRuleReconciler) SetupWithManager(mgr ctrl.Manager) error {
// 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 false
//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 &&
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,73 @@ 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)
}

_, err = r.Reconcile(context.TODO(), req)
if err != nil {
t.Fatalf("reconcile after updating addondeploymentconfig: (%v)", 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 +379,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 +622,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",
},
},
}
}
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

0 comments on commit f2e9ad1

Please sign in to comment.