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

feat: support custom names for generated k8s resources #3537

Merged
merged 7 commits into from
Jun 7, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

if mergeGateways {
return GatewayClassOwnerLabel(string(gateway.Spec.GatewayClassName))

Check warning on line 234 in internal/gatewayapi/helpers.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/helpers.go#L232-L234

Added lines #L232 - L234 were not covered by tests
}

return GatewayOwnerLabels(gateway.Namespace, gateway.Name)

Check warning on line 237 in internal/gatewayapi/helpers.go

View check run for this annotation

Codecov / codecov/patch

internal/gatewayapi/helpers.go#L237

Added line #L237 was not covered by tests
}

// 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
Loading