Skip to content

Commit

Permalink
Add cluster name and namespace to BundleDeployment
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Manno committed Jun 5, 2023
1 parent 538497d commit 5510982
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 38 deletions.
17 changes: 12 additions & 5 deletions integrationtests/controller/bundle/bundle_labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ var _ = Describe("Bundle labels", func() {

By("BundleDeployment has the foo label from Bundle")
Eventually(func() bool {
return expectedLabelValue(bundleDeploymentController, bdLabels, "foo", "bar")
bd, ok := expectedLabelValue(bundleDeploymentController, bdLabels, "foo", "bar")
if !ok {
return false
}
Expect(bd.Labels).To(HaveKeyWithValue("fleet.cattle.io/cluster", "cluster"))
Expect(bd.Labels).To(HaveKeyWithValue("fleet.cattle.io/cluster-namespace", env.namespace))
return true
}).Should(BeTrue())

By("Modifying foo label in Bundle")
Expand All @@ -77,18 +83,19 @@ var _ = Describe("Bundle labels", func() {

By("Should modify foo label in BundleDeployment")
Eventually(func() bool {
return expectedLabelValue(bundleDeploymentController, bdLabels, "foo", "modified")
_, ok := expectedLabelValue(bundleDeploymentController, bdLabels, "foo", "modified")
return ok
}).Should(BeTrue())

})
})
})

func expectedLabelValue(controller v1gen.BundleDeploymentController, bdLabels map[string]string, key, value string) bool {
func expectedLabelValue(controller v1gen.BundleDeploymentController, bdLabels map[string]string, key, value string) (*v1alpha1.BundleDeployment, bool) {
list, err := controller.List("", metav1.ListOptions{LabelSelector: labels.SelectorFromSet(bdLabels).String()})
Expect(err).NotTo(HaveOccurred())
if len(list.Items) == 1 {
return list.Items[0].Labels[key] == value
return &list.Items[0], list.Items[0].Labels[key] == value
}
return false
return nil, false
}
6 changes: 3 additions & 3 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ type Options struct {
NoCA bool // unused
}

// AgentWithConfig writes the agent manifest to the given writer. It
// includes an updated agent token secret from the cluster. It finds or creates
// the agent config inside a configmap.
// AgentWithConfig returns the agent manifest. It includes an updated agent
// token secret from the cluster. It finds or creates the agent config inside a
// configmap.
//
// This is used when importing a cluster.
func AgentWithConfig(ctx context.Context, agentNamespace, controllerNamespace, agentScope string, cg *client.Getter, tokenName string, opts *Options) ([]runtime.Object, error) {
Expand Down
20 changes: 17 additions & 3 deletions pkg/apis/fleet.cattle.io/v1alpha1/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,26 @@ import (
)

var (
ClusterConditionReady = "Ready"
ClusterNamespaceAnnotation = "fleet.cattle.io/cluster-namespace"
ClusterConditionReady = "Ready"
// ClusterNamespaceAnnotation used on a cluster namespace to refer to
// the cluster registration namespace, which contains the cluster
// resource.
ClusterNamespaceAnnotation = "fleet.cattle.io/cluster-namespace"
// ClusterAnnotation used on a cluster namespace to refer to the
// cluster name for that namespace.
ClusterAnnotation = "fleet.cattle.io/cluster"
ClusterRegistrationAnnotation = "fleet.cattle.io/cluster-registration"
ClusterRegistrationNamespaceAnnotation = "fleet.cattle.io/cluster-registration-namespace"
ManagedLabel = "fleet.cattle.io/managed"
// ManagedLabel is used for clean up. Cluster namespaces and other
// resources with this label will be cleaned up. Used in Rancher to
// identify fleet namespaces.
ManagedLabel = "fleet.cattle.io/managed"
// ClusterNamespaceLabel is used on a bundledeployment to refer to the
// cluster registration namespace of the targeted cluster.
ClusterNamespaceLabel = "fleet.cattle.io/cluster-namespace"
// ClusterLabel is used on a bundledeployment to refer to the targeted
// cluster
ClusterLabel = "fleet.cattle.io/cluster"
)

// +genclient
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/bundle/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func bundleDeployments(targets []*target.Target, bundle *fleet.Bundle) (result [
ObjectMeta: v1.ObjectMeta{
Name: target.Deployment.Name,
Namespace: target.Deployment.Namespace,
Labels: target.BundleDeploymentLabels(),
Labels: target.BundleDeploymentLabels(target.Cluster.Namespace, target.Cluster.Name),
},
Spec: target.Deployment.Spec,
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/cleanup/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (h *handler) cleanupNamespace(key string, obj *corev1.Namespace) (*corev1.N

logrus.Debugf("Cleaning up fleet-managed namespace %s", obj.Name)

// NOTE no cluster has this annotation - is this dead code?
// check if the cluster for this cluster namespace still exists, otherwise clean up the namespace
_, err := h.clusters.Get(obj.Annotations[fleet.ClusterNamespaceAnnotation], obj.Annotations[fleet.ClusterAnnotation])
if apierrors.IsNotFound(err) {
err = h.namespaces.Delete(key, nil)
Expand Down
56 changes: 31 additions & 25 deletions pkg/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,26 +391,6 @@ func (m *Manager) foldInDeployments(bundle *fleet.Bundle, targets []*Target) err
return nil
}

func deploymentLabelsForNewBundle(bundle *fleet.Bundle) map[string]string {
labels := yaml.CleanAnnotationsForExport(bundle.Labels)
for k, v := range bundle.Labels {
if strings.HasPrefix(k, "fleet.cattle.io/") {
labels[k] = v
}
}
for k, v := range deploymentLabelsForSelector(bundle) {
labels[k] = v
}
return labels
}

func deploymentLabelsForSelector(bundle *fleet.Bundle) map[string]string {
return map[string]string{
"fleet.cattle.io/bundle-name": bundle.Name,
"fleet.cattle.io/bundle-namespace": bundle.Namespace,
}
}

type Target struct {
Deployment *fleet.BundleDeployment
ClusterGroups []*fleet.ClusterGroup
Expand All @@ -431,22 +411,48 @@ func (t *Target) ResetDeployment() {
ObjectMeta: metav1.ObjectMeta{
Name: t.Bundle.Name,
Namespace: t.Cluster.Status.Namespace,
Labels: t.BundleDeploymentLabels(),
Labels: t.BundleDeploymentLabels(t.Cluster.Namespace, t.Cluster.Name),
},
}
}

// BundleDeploymentLabels returns all labels from the Bundle
func (t *Target) BundleDeploymentLabels() map[string]string {
labels := map[string]string{}
for k, v := range deploymentLabelsForNewBundle(t.Bundle) {
// BundleDeploymentLabels builds all labels for a bundledeployment
func (t *Target) BundleDeploymentLabels(clusterNamespace string, clusterName string) map[string]string {
// remove labels starting with kubectl.kubernetes.io or containing
// cattle.io from bundle
labels := yaml.CleanAnnotationsForExport(t.Bundle.Labels)

// copy fleet labels from bundle to bundledeployment
for k, v := range t.Bundle.Labels {
if strings.HasPrefix(k, "fleet.cattle.io/") {
labels[k] = v
}
}

// labels for the bundledeployment by bundle selector
for k, v := range deploymentLabelsForSelector(t.Bundle) {
labels[k] = v
}

// ManagedLabel allows clean up of the bundledeployment
labels[fleet.ManagedLabel] = "true"

// add labels to identify the cluster this bundledeployment belongs to
labels[fleet.ClusterNamespaceLabel] = clusterNamespace
labels[fleet.ClusterLabel] = clusterName

return labels
}

// deploymentLabelsForSelector returns the labels that are used to select
// bundledeployments for a given bundle
func deploymentLabelsForSelector(bundle *fleet.Bundle) map[string]string {
return map[string]string{
"fleet.cattle.io/bundle-name": bundle.Name,
"fleet.cattle.io/bundle-namespace": bundle.Namespace,
}
}

// getRollout returns the rollout strategy for the specified targets (pure function)
func getRollout(targets []*Target) *fleet.RolloutStrategy {
var rollout *fleet.RolloutStrategy
Expand Down

0 comments on commit 5510982

Please sign in to comment.