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 standard Crossplane package labels to objects. #5536

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions internal/controller/pkg/revision/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func (b *RuntimeManifestBuilder) ServiceAccount(overrides ...ServiceAccountOverr

// Overrides that we are opinionated about.
ServiceAccountWithNamespace(b.namespace),
ServiceAccountWithLabels(b.podSelectors()),
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we should rename podSelectors if we're now using it to do more than select pods.

ServiceAccountWithOwnerReferences([]metav1.OwnerReference{meta.AsController(meta.TypedReferenceTo(b.revision, b.revision.GetObjectKind().GroupVersionKind()))}),
ServiceAccountWithAdditionalPullSecrets(append(b.revision.GetPackagePullSecrets(), b.serviceAccountPullSecrets...)),
)
Expand Down Expand Up @@ -194,6 +195,7 @@ func (b *RuntimeManifestBuilder) Deployment(serviceAccount string, overrides ...
// Optional defaults, will be used only if the runtime config does not
// specify them.
DeploymentWithOptionalName(b.revision.GetName()),
DeploymentWithLabels(b.podSelectors()),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure if adding it as an override is a good option. Other option is to add it in the runtime pre/post hook.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure either. @turkenh introduced this pattern, so I'm sure he can provide guidance.

DeploymentWithOptionalReplicas(1),
DeploymentWithOptionalPodSecurityContext(&corev1.PodSecurityContext{
RunAsNonRoot: &runAsNonRoot,
Expand Down Expand Up @@ -270,6 +272,7 @@ func (b *RuntimeManifestBuilder) Service(overrides ...ServiceOverride) *corev1.S
// Overrides that we are opinionated about.
ServiceWithNamespace(b.namespace),
ServiceWithOwnerReferences([]metav1.OwnerReference{meta.AsController(meta.TypedReferenceTo(b.revision, b.revision.GetObjectKind().GroupVersionKind()))}),
ServiceWithLabels(b.podSelectors()),
ServiceWithSelectors(b.podSelectors()),
ServiceWithAdditionalPorts([]corev1.ServicePort{
{
Expand Down Expand Up @@ -300,6 +303,7 @@ func (b *RuntimeManifestBuilder) TLSClientSecret() *corev1.Secret {
ObjectMeta: metav1.ObjectMeta{
Name: *b.revision.GetTLSClientSecretName(),
Namespace: b.namespace,
Labels: b.podSelectors(),
OwnerReferences: []metav1.OwnerReference{meta.AsController(meta.TypedReferenceTo(b.revision, b.revision.GetObjectKind().GroupVersionKind()))},
},
}
Expand All @@ -315,6 +319,7 @@ func (b *RuntimeManifestBuilder) TLSServerSecret() *corev1.Secret {
ObjectMeta: metav1.ObjectMeta{
Name: *b.revision.GetTLSServerSecretName(),
Namespace: b.namespace,
Labels: b.podSelectors(),
OwnerReferences: []metav1.OwnerReference{meta.AsController(meta.TypedReferenceTo(b.revision, b.revision.GetObjectKind().GroupVersionKind()))},
},
}
Expand Down
36 changes: 36 additions & 0 deletions internal/controller/pkg/revision/runtime_override_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@
}
}

// ServiceAccountWithLabels appends to the labels of a ServiceAccount.
func ServiceAccountWithLabels(labels map[string]string) ServiceAccountOverride {
return func(sa *corev1.ServiceAccount) {
if sa.Labels == nil {

Check failure on line 59 in internal/controller/pkg/revision/runtime_override_options.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/crossplane/crossplane-runtime) -s prefix(github.com/crossplane/crossplane) -s blank -s dot --custom-order (gci)
sa.Labels = map[string]string{}
}
for k, v := range labels {
sa.Labels[k] = v
}
}
}

// ServiceAccountWithOwnerReferences overrides the owner references of a
// ServiceAccount.
func ServiceAccountWithOwnerReferences(owners []metav1.OwnerReference) ServiceAccountOverride {
Expand Down Expand Up @@ -85,6 +97,18 @@
}
}

// DeploymentWithLabels appends to Labels.
func DeploymentWithLabels(labels map[string]string) DeploymentOverride {
return func(d *appsv1.Deployment) {
if d.Labels == nil {
d.Labels = map[string]string{}
}
for k, v := range labels {
d.Labels[k] = v
}
}
}

// DeploymentWithNamespace overrides the namespace of a Deployment.
func DeploymentWithNamespace(namespace string) DeploymentOverride {
return func(d *appsv1.Deployment) {
Expand Down Expand Up @@ -357,6 +381,18 @@
}
}

// ServiceWithLabels appends to Labels.
func ServiceWithLabels(labels map[string]string) ServiceOverride {
return func(s *corev1.Service) {
if s.Labels == nil {
s.Labels = map[string]string{}
}
for k, v := range labels {
s.Labels[k] = v
}
}
}

// ServiceWithNamespace overrides the namespace of a Service.
func ServiceWithNamespace(namespace string) ServiceOverride {
return func(s *corev1.Service) {
Expand Down
4 changes: 4 additions & 0 deletions internal/controller/pkg/revision/runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ func deploymentProvider(provider string, revision string, image string, override
ObjectMeta: metav1.ObjectMeta{
Name: revision,
Namespace: namespace,
Labels: map[string]string{
"pkg.crossplane.io/revision": revision,
"pkg.crossplane.io/provider": provider,
},
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "pkg.crossplane.io/v1",
Expand Down