Skip to content

Commit

Permalink
feat: support custom names for generated k8s resources (envoyproxy#3537)
Browse files Browse the repository at this point in the history
* feat: support custom names for generated k8s resources

Relates to envoyproxy#3527

Signed-off-by: Arko Dasgupta <arko@tetrate.io>

* use labels instead of name and ns to get deployment and svc

Signed-off-by: Arko Dasgupta <arko@tetrate.io>
  • Loading branch information
arkodg authored Jun 7, 2024
1 parent 92760c8 commit 33fceb0
Show file tree
Hide file tree
Showing 10 changed files with 750 additions and 32 deletions.
9 changes: 9 additions & 0 deletions internal/gatewayapi/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ func GatewayClassOwnerLabel(name string) map[string]string {
return map[string]string{OwningGatewayClassLabel: name}
}

// OwnerLabels returns the owner labels based on the mergeGateways setting
func OwnerLabels(gateway *gwapiv1.Gateway, mergeGateways bool) map[string]string {
if mergeGateways {
return GatewayClassOwnerLabel(string(gateway.Spec.GatewayClassName))
}

return GatewayOwnerLabels(gateway.Namespace, gateway.Name)
}

// servicePortToContainerPort translates a service port into an ephemeral
// container port.
func servicePortToContainerPort(servicePort int32, envoyProxy *egv1a1.EnvoyProxy) int32 {
Expand Down
33 changes: 29 additions & 4 deletions internal/infrastructure/kubernetes/proxy/resource_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,19 @@ func (r *ResourceRender) Service() (*corev1.Service, error) {
},
ObjectMeta: metav1.ObjectMeta{
Namespace: r.Namespace,
Name: r.Name(),
Labels: labels,
Annotations: annotations,
},
Spec: serviceSpec,
}

// set name
if envoyServiceConfig.Name != nil {
svc.ObjectMeta.Name = *envoyServiceConfig.Name
} else {
svc.ObjectMeta.Name = r.Name()
}

// apply merge patch to service
var err error
if svc, err = envoyServiceConfig.ApplyMergePatch(svc); err != nil {
Expand Down Expand Up @@ -225,7 +231,6 @@ func (r *ResourceRender) Deployment() (*appsv1.Deployment, error) {
},
ObjectMeta: metav1.ObjectMeta{
Namespace: r.Namespace,
Name: r.Name(),
Labels: dpLabels,
Annotations: dpAnnotations,
},
Expand Down Expand Up @@ -261,6 +266,13 @@ func (r *ResourceRender) Deployment() (*appsv1.Deployment, error) {
},
}

// set name
if deploymentConfig.Name != nil {
deployment.ObjectMeta.Name = *deploymentConfig.Name
} else {
deployment.ObjectMeta.Name = r.Name()
}

// omit the deployment replicas if HPA is being set
if provider.GetEnvoyProxyKubeProvider().EnvoyHpa != nil {
deployment.Spec.Replicas = nil
Expand Down Expand Up @@ -314,7 +326,6 @@ func (r *ResourceRender) DaemonSet() (*appsv1.DaemonSet, error) {
},
ObjectMeta: metav1.ObjectMeta{
Namespace: r.Namespace,
Name: r.Name(),
Labels: dsLabels,
Annotations: dsAnnotations,
},
Expand All @@ -331,6 +342,13 @@ func (r *ResourceRender) DaemonSet() (*appsv1.DaemonSet, error) {
},
}

// set name
if daemonSetConfig.Name != nil {
daemonSet.ObjectMeta.Name = *daemonSetConfig.Name
} else {
daemonSet.ObjectMeta.Name = r.Name()
}

// apply merge patch to daemonset
if daemonSet, err = daemonSetConfig.ApplyMergePatch(daemonSet); err != nil {
return nil, err
Expand Down Expand Up @@ -365,7 +383,6 @@ func (r *ResourceRender) HorizontalPodAutoscaler() (*autoscalingv2.HorizontalPod
ScaleTargetRef: autoscalingv2.CrossVersionObjectReference{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: r.Name(),
},
MinReplicas: hpaConfig.MinReplicas,
MaxReplicas: ptr.Deref(hpaConfig.MaxReplicas, 1),
Expand All @@ -374,6 +391,14 @@ func (r *ResourceRender) HorizontalPodAutoscaler() (*autoscalingv2.HorizontalPod
},
}

// set deployment target ref name
deploymentConfig := provider.GetEnvoyProxyKubeProvider().EnvoyDeployment
if deploymentConfig.Name != nil {
hpa.Spec.ScaleTargetRef.Name = *deploymentConfig.Name
} else {
hpa.Spec.ScaleTargetRef.Name = r.Name()
}

return hpa, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,13 @@ func TestDeployment(t *testing.T) {
},
},
},
{
caseName: "with-name",
infra: newTestInfra(),
deploy: &egv1a1.KubernetesDeploymentSpec{
Name: ptr.To("custom-deployment-name"),
},
},
}
for _, tc := range cases {
t.Run(tc.caseName, func(t *testing.T) {
Expand Down Expand Up @@ -933,6 +940,13 @@ func TestDaemonSet(t *testing.T) {
infra: newTestInfra(),
extraArgs: []string{"--key1 val1", "--key2 val2"},
},
{
caseName: "with-name",
infra: newTestInfra(),
daemonset: &egv1a1.KubernetesDaemonSetSpec{
Name: ptr.To("custom-daemonset-name"),
},
},
}
for _, tc := range cases {
t.Run(tc.caseName, func(t *testing.T) {
Expand Down Expand Up @@ -1083,6 +1097,13 @@ func TestService(t *testing.T) {
},
},
},
{
caseName: "with-name",
infra: newTestInfra(),
service: &egv1a1.KubernetesServiceSpec{
Name: ptr.To("custom-service-name"),
},
},
}
for _, tc := range cases {
t.Run(tc.caseName, func(t *testing.T) {
Expand Down Expand Up @@ -1207,6 +1228,7 @@ func TestHorizontalPodAutoscaler(t *testing.T) {
caseName string
infra *ir.Infra
hpa *egv1a1.KubernetesHorizontalPodAutoscalerSpec
deploy *egv1a1.KubernetesDeploymentSpec
}{
{
caseName: "default",
Expand Down Expand Up @@ -1245,6 +1267,17 @@ func TestHorizontalPodAutoscaler(t *testing.T) {
},
},
},
{
caseName: "with-deployment-name",
infra: newTestInfra(),
hpa: &egv1a1.KubernetesHorizontalPodAutoscalerSpec{
MinReplicas: ptr.To[int32](5),
MaxReplicas: ptr.To[int32](10),
},
deploy: &egv1a1.KubernetesDeploymentSpec{
Name: ptr.To("custom-deployment-name"),
},
},
}

for _, tc := range cases {
Expand All @@ -1255,7 +1288,9 @@ func TestHorizontalPodAutoscaler(t *testing.T) {
if tc.hpa != nil {
provider.Kubernetes.EnvoyHpa = tc.hpa
}

if tc.deploy != nil {
provider.Kubernetes.EnvoyDeployment = tc.deploy
}
provider.GetEnvoyProxyKubeProvider()

r := NewResourceRender(cfg.Namespace, tc.infra.GetProxyInfra(), cfg.EnvoyGateway)
Expand Down
Loading

0 comments on commit 33fceb0

Please sign in to comment.