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

Bug 2077897: Set custom endpoint environment variable if available #155

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions pkg/operator/starter.go
Expand Up @@ -135,6 +135,7 @@ func RunOperator(ctx context.Context, controllerConfig *controllercmd.Controller
csidrivercontrollerservicecontroller.WithReplicasHook(nodeInformer.Lister()),
withCustomCABundle(cloudConfigLister),
withCustomTags(infraInformer.Lister()),
withCustomEndPoint(infraInformer.Lister()),
).WithCSIDriverNodeService(
"AWSEBSDriverNodeServiceController",
assets.ReadFile,
Expand Down Expand Up @@ -220,6 +221,41 @@ func withCustomCABundle(cloudConfigLister corev1listers.ConfigMapNamespaceLister
}
}

func withCustomEndPoint(infraLister v1.InfrastructureLister) deploymentcontroller.DeploymentHookFunc {
return func(_ *opv1.OperatorSpec, deployment *appsv1.Deployment) error {
infra, err := infraLister.Get(infrastructureName)
if err != nil {
return err
}
if infra.Status.PlatformStatus == nil || infra.Status.PlatformStatus.AWS == nil {
return nil
}
serviceEndPoints := infra.Status.PlatformStatus.AWS.ServiceEndpoints
ec2EndPoint := ""
for _, serviceEndPoint := range serviceEndPoints {
if serviceEndPoint.Name == "ec2" {
ec2EndPoint = serviceEndPoint.URL
}
}
if ec2EndPoint == "" {
return nil
}

for i := range deployment.Spec.Template.Spec.Containers {
container := &deployment.Spec.Template.Spec.Containers[i]
if container.Name != "csi-driver" {
continue
}
container.Env = append(container.Env, corev1.EnvVar{
Name: "AWS_EC2_ENDPOINT",
Value: ec2EndPoint,
})
return nil
}
return nil
}
}

func newCustomCABundleSyncer(
operatorClient v1helpers.OperatorClient,
kubeInformers v1helpers.KubeInformersForNamespaces,
Expand Down
129 changes: 129 additions & 0 deletions pkg/operator/starter_test.go
Expand Up @@ -276,3 +276,132 @@ func TestWithCustomTags(t *testing.T) {
})
}
}

func TestWithCustomEndPoint(t *testing.T) {
tests := []struct {
name string
customEndPoints []v1.AWSServiceEndpoint
inDeployment *appsv1.Deployment
expected *appsv1.Deployment
}{
{
name: "when no service end point is set",
customEndPoints: []v1.AWSServiceEndpoint{},
inDeployment: &appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: "csi-driver",
Env: []corev1.EnvVar{
{
Name: "AWS_SECRET",
Value: "SECRET",
},
},
}},
},
},
},
},
expected: &appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: "csi-driver",
Env: []corev1.EnvVar{
{
Name: "AWS_SECRET",
Value: "SECRET",
},
},
}},
},
},
},
},
},
{
name: "when a custom ec2 end point is specified",
customEndPoints: []v1.AWSServiceEndpoint{
{
Name: "ec2",
URL: "https://example.com",
},
},
inDeployment: &appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: "csi-driver",
Env: []corev1.EnvVar{
{
Name: "AWS_SECRET",
Value: "SECRET",
},
},
}},
},
},
},
},
expected: &appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: "csi-driver",
Env: []corev1.EnvVar{
{
Name: "AWS_SECRET",
Value: "SECRET",
},
{
Name: "AWS_EC2_ENDPOINT",
Value: "https://example.com",
},
},
}},
},
},
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
infra := &v1.Infrastructure{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster",
},
Status: v1.InfrastructureStatus{
PlatformStatus: &v1.PlatformStatus{
AWS: &v1.AWSPlatformStatus{
ServiceEndpoints: test.customEndPoints,
},
},
},
}
configClient := fakeconfig.NewSimpleClientset(infra)
configInformerFactory := configinformers.NewSharedInformerFactory(configClient, 0)
configInformerFactory.Config().V1().Infrastructures().Informer().GetIndexer().Add(infra)
stopCh := make(chan struct{})
go configInformerFactory.Start(stopCh)
defer close(stopCh)
wait.Poll(100*time.Millisecond, 30*time.Second, func() (bool, error) {
return configInformerFactory.Config().V1().Infrastructures().Informer().HasSynced(), nil
})
deployment := test.inDeployment.DeepCopy()
err := withCustomEndPoint(configInformerFactory.Config().V1().Infrastructures().Lister())(nil, deployment)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if e, a := test.expected, deployment; !equality.Semantic.DeepEqual(e, a) {
t.Errorf("unexpected deployment\nwant=%#v\ngot= %#v", e, a)
}
})
}

}