Skip to content

Commit

Permalink
Propagate labels from bundle to bundledeployment for tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Aug 28, 2020
1 parent b760b02 commit 5489992
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
45 changes: 36 additions & 9 deletions pkg/controllers/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,39 @@ package git
import (
"context"
"encoding/json"
"sort"
"time"

fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/fleet/pkg/config"
fleetcontrollers "github.com/rancher/fleet/pkg/generated/controllers/fleet.cattle.io/v1alpha1"
"github.com/rancher/fleet/pkg/summary"
gitjob "github.com/rancher/gitjob/pkg/apis/gitjob.cattle.io/v1"
v1 "github.com/rancher/gitjob/pkg/generated/controllers/gitjob.cattle.io/v1"
"github.com/rancher/wrangler/pkg/apply"
"github.com/rancher/wrangler/pkg/name"
"github.com/rancher/wrangler/pkg/relatedresource"
"github.com/rancher/wrangler/pkg/yaml"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)

func Register(ctx context.Context, apply apply.Apply, gitJobs v1.GitJobController, gitRepos fleetcontrollers.GitRepoController) {
var (
RepoLabel = "fleet.cattle.io/repo-name"
)

func Register(ctx context.Context,
apply apply.Apply,
gitJobs v1.GitJobController,
bundleDeployments fleetcontrollers.BundleDeploymentCache,
gitRepos fleetcontrollers.GitRepoController) {
h := &handler{
gitjobCache: gitJobs.Cache(),
gitjobCache: gitJobs.Cache(),
bundleDeployments: bundleDeployments,
}

fleetcontrollers.RegisterGitRepoGeneratingHandler(ctx, gitRepos, apply, "", "gitjobs", h.OnChange, nil)
Expand All @@ -31,12 +44,25 @@ func Register(ctx context.Context, apply apply.Apply, gitJobs v1.GitJobControlle
}

type handler struct {
gitjobCache v1.GitJobCache
gitjobCache v1.GitJobCache
bundleDeployments fleetcontrollers.BundleDeploymentCache
}

func targetsOrDefault(targets []fleet.GitTarget) []fleet.GitTarget {
if len(targets) == 0 {
return []fleet.GitTarget{
{
Name: "default",
ClusterGroup: "default",
},
}
}
return targets
}

func (h *handler) getConfig(repo *fleet.GitRepo) (*corev1.ConfigMap, error) {
spec := &fleet.BundleSpec{}
for _, target := range repo.Spec.Targets {
for _, target := range targetsOrDefault(repo.Spec.Targets) {
spec.Targets = append(spec.Targets, fleet.BundleTarget{
Name: target.Name,
ClusterSelector: target.ClusterSelector,
Expand Down Expand Up @@ -145,14 +171,15 @@ func (h *handler) OnChange(gitrepo *fleet.GitRepo, status fleet.GitRepoStatus) (
},
&gitjob.GitJob{
ObjectMeta: metav1.ObjectMeta{
Name: gitrepo.Name,
Namespace: gitrepo.Namespace,
Labels: yaml.CleanAnnotationsForExport(gitrepo.Labels),
Annotations: yaml.CleanAnnotationsForExport(gitrepo.Annotations),
Name: gitrepo.Name,
Namespace: gitrepo.Namespace,
},
Spec: gitjob.GitJobSpec{
Git: gitjob.GitInfo{
Credential: gitjob.Credential{
GitSecretName: gitrepo.Spec.ClientSecretName,
GitHostname: "github.com",
ClientSecretName: gitrepo.Spec.ClientSecretName,
},
Provider: "polling",
Repo: gitrepo.Spec.Repo,
Expand Down Expand Up @@ -188,7 +215,7 @@ func (h *handler) OnChange(gitrepo *fleet.GitRepo, status fleet.GitRepoStatus) (
"fleet",
"apply",
"--targets-file=/run/config/targets.yaml",
"--label=fleet.cattle.io/repo-name=" + gitrepo.Name,
"--label=" + RepoLabel + "=" + gitrepo.Name,
"--namespace", gitrepo.Namespace,
"--service-account", gitrepo.Spec.ServiceAccount,
gitrepo.Name,
Expand Down
19 changes: 16 additions & 3 deletions pkg/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strconv"
"strings"

"github.com/rancher/wrangler/pkg/yaml"

"github.com/pkg/errors"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/fleet/pkg/bundle"
Expand Down Expand Up @@ -242,7 +244,7 @@ func (m *Manager) Targets(fleetBundle *fleet.Bundle) (result []*Target, _ error)
}

func (m *Manager) foldInDeployments(app *fleet.Bundle, targets []*Target) error {
bundleDeployments, err := m.bundleDeploymentCache.List("", labels.SelectorFromSet(DeploymentLabels(app)))
bundleDeployments, err := m.bundleDeploymentCache.List("", labels.SelectorFromSet(DeploymentLabelsForSelector(app)))
if err != nil {
return err
}
Expand All @@ -259,7 +261,15 @@ func (m *Manager) foldInDeployments(app *fleet.Bundle, targets []*Target) error
return nil
}

func DeploymentLabels(app *fleet.Bundle) map[string]string {
func DeploymentLabelsForNewBundle(app *fleet.Bundle) map[string]string {
labels := yaml.CleanAnnotationsForExport(app.Labels)
for k, v := range DeploymentLabelsForSelector(app) {
labels[k] = v
}
return labels
}

func DeploymentLabelsForSelector(app *fleet.Bundle) map[string]string {
return map[string]string{
"fleet.cattle.io/bundle-name": app.Name,
"fleet.cattle.io/bundle-namespace": app.Namespace,
Expand All @@ -286,7 +296,10 @@ func (t *Target) AssignNewDeployment() {
ObjectMeta: metav1.ObjectMeta{
Name: t.Bundle.Name,
Namespace: t.Cluster.Status.Namespace,
Labels: DeploymentLabels(t.Bundle),
Labels: DeploymentLabelsForNewBundle(t.Bundle),
Annotations: map[string]string{
fleet.ManagedAnnotation: "true",
},
},
}
}
Expand Down

0 comments on commit 5489992

Please sign in to comment.