Skip to content

Commit

Permalink
initial commit for label matching for deps
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrokethecloud committed Sep 28, 2021
1 parent f6e3145 commit 08414e2
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 18 deletions.
130 changes: 130 additions & 0 deletions charts/fleet-crd/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,49 @@ spec:
properties:
spec:
properties:
additionalBundleLabels:
additionalProperties:
nullable: true
type: string
nullable: true
type: object
customBundleName:
nullable: true
type: string
defaultNamespace:
nullable: true
type: string
dependsOn:
items:
properties:
bundleSelector:
nullable: true
properties:
matchExpressions:
items:
properties:
key:
nullable: true
type: string
operator:
nullable: true
type: string
values:
items:
nullable: true
type: string
nullable: true
type: array
type: object
nullable: true
type: array
matchLabels:
additionalProperties:
nullable: true
type: string
nullable: true
type: object
type: object
name:
nullable: true
type: string
Expand Down Expand Up @@ -869,6 +906,34 @@ spec:
dependsOn:
items:
properties:
bundleSelector:
nullable: true
properties:
matchExpressions:
items:
properties:
key:
nullable: true
type: string
operator:
nullable: true
type: string
values:
items:
nullable: true
type: string
nullable: true
type: array
type: object
nullable: true
type: array
matchLabels:
additionalProperties:
nullable: true
type: string
nullable: true
type: object
type: object
name:
nullable: true
type: string
Expand Down Expand Up @@ -2640,12 +2705,49 @@ spec:
properties:
spec:
properties:
additionalBundleLabels:
additionalProperties:
nullable: true
type: string
nullable: true
type: object
customBundleName:
nullable: true
type: string
defaultNamespace:
nullable: true
type: string
dependsOn:
items:
properties:
bundleSelector:
nullable: true
properties:
matchExpressions:
items:
properties:
key:
nullable: true
type: string
operator:
nullable: true
type: string
values:
items:
nullable: true
type: string
nullable: true
type: array
type: object
nullable: true
type: array
matchLabels:
additionalProperties:
nullable: true
type: string
nullable: true
type: object
type: object
name:
nullable: true
type: string
Expand Down Expand Up @@ -3485,6 +3587,34 @@ spec:
dependsOn:
items:
properties:
bundleSelector:
nullable: true
properties:
matchExpressions:
items:
properties:
key:
nullable: true
type: string
operator:
nullable: true
type: string
values:
items:
nullable: true
type: string
nullable: true
type: array
type: object
nullable: true
type: array
matchLabels:
additionalProperties:
nullable: true
type: string
nullable: true
type: object
type: object
name:
nullable: true
type: string
Expand Down
2 changes: 1 addition & 1 deletion cmd/fleetagent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type FleetAgent struct {

func (a *FleetAgent) Run(cmd *cobra.Command, args []string) error {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
log.Println(http.ListenAndServe("localhost:6061", nil))
}()

var (
Expand Down
26 changes: 18 additions & 8 deletions modules/agent/pkg/controllers/bundledeployment/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ func (h *handler) Cleanup(key string, bd *fleet.BundleDeployment) (*fleet.Bundle
}

func (h *handler) DeployBundle(bd *fleet.BundleDeployment, status fleet.BundleDeploymentStatus) (fleet.BundleDeploymentStatus, error) {
dependOn, ok, err := h.checkDependency(bd)
message, ok, err := h.checkDependency(bd)
if err != nil {
return status, err
}

if !ok {
return status, fmt.Errorf("bundle %s has dependent bundle %s that is not ready", bd.Name, dependOn)
return status, fmt.Errorf("bundle %s has %s", bd.Name, message)
}

release, err := h.deployManager.Deploy(bd)
Expand All @@ -111,12 +111,17 @@ func (h *handler) DeployBundle(bd *fleet.BundleDeployment, status fleet.BundleDe
func (h *handler) checkDependency(bd *fleet.BundleDeployment) (string, bool, error) {
bundleNamespace := bd.Labels["fleet.cattle.io/bundle-namespace"]
for _, depend := range bd.Spec.DependsOn {
ls := &metav1.LabelSelector{
MatchLabels: map[string]string{
"fleet.cattle.io/bundle-name": depend.Name,
"fleet.cattle.io/bundle-namespace": bundleNamespace,
},
ls := &metav1.LabelSelector{}

if depend.BundleSelector != nil {
ls = depend.BundleSelector
}

if depend.Name != "" {
ls = metav1.AddLabelToSelector(ls, "fleet.cattle.io/bundle-name", depend.Name)
ls = metav1.AddLabelToSelector(ls, "fleet.cattle.io/bundle-namespace", bundleNamespace)
}

selector, err := metav1.LabelSelectorAsSelector(ls)
if err != nil {
return "", false, err
Expand All @@ -125,12 +130,17 @@ func (h *handler) checkDependency(bd *fleet.BundleDeployment) (string, bool, err
if err != nil {
return "", false, err
}

if len(bds) == 0 {
return fmt.Sprintf("no bundles matching labels %s in namespace %s", selector.String(), bundleNamespace), false, nil
}

for _, bd := range bds {
c := condition.Cond("Ready")
if c.IsTrue(bd) {
continue
} else {
return fmt.Sprintf("%s/%s", bundleNamespace, depend.Name), false, nil
return fmt.Sprintf("dependent bundles matching labels %s in namespace %s not ready", selector.String(), bundleNamespace), false, nil
}
}
}
Expand Down
17 changes: 10 additions & 7 deletions pkg/apis/fleet.cattle.io/v1alpha1/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,19 @@ type BundleNamespaceMapping struct {
type BundleSpec struct {
BundleDeploymentOptions

Paused bool `json:"paused,omitempty"`
RolloutStrategy *RolloutStrategy `json:"rolloutStrategy,omitempty"`
Resources []BundleResource `json:"resources,omitempty"`
Targets []BundleTarget `json:"targets,omitempty"`
TargetRestrictions []BundleTargetRestriction `json:"targetRestrictions,omitempty"`
DependsOn []BundleRef `json:"dependsOn,omitempty"`
Paused bool `json:"paused,omitempty"`
RolloutStrategy *RolloutStrategy `json:"rolloutStrategy,omitempty"`
Resources []BundleResource `json:"resources,omitempty"`
Targets []BundleTarget `json:"targets,omitempty"`
TargetRestrictions []BundleTargetRestriction `json:"targetRestrictions,omitempty"`
DependsOn []BundleRef `json:"dependsOn,omitempty"`
CustomBundleName string `json:"customBundleName,omitempty"`
AdditionalBundleLabels map[string]string `json:"additionalBundleLabels,omitempty"`
}

type BundleRef struct {
Name string `json:"name,omitempty"`
Name string `json:"name,omitempty"`
BundleSelector *metav1.LabelSelector `json:"bundleSelector,omitempty"`
}

type BundleResource struct {
Expand Down
20 changes: 18 additions & 2 deletions pkg/apis/fleet.cattle.io/v1alpha1/zz_generated_deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions pkg/bundle/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ func read(ctx context.Context, name, baseDir string, bundleSpecReader io.Reader,
}

meta.Name = name
if bundle.BundleSpec.CustomBundleName != "" {
meta.Name = bundle.BundleSpec.CustomBundleName
}

setTargetNames(&bundle.BundleSpec)

resources, err := readResources(ctx, &bundle.BundleSpec, opts.Compress, baseDir, opts.Auth)
Expand All @@ -174,6 +178,11 @@ func read(ctx context.Context, name, baseDir string, bundleSpecReader io.Reader,
def.Labels[k] = v
}

// apply additional labels from spec
for k, v := range bundle.BundleSpec.AdditionalBundleLabels {
def.Labels[k] = v
}

if opts.ServiceAccount != "" {
def.Spec.ServiceAccount = opts.ServiceAccount
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/controllers/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,5 +631,13 @@ func argsAndEnvs(gitrepo *fleet.GitRepo) ([]string, []corev1.EnvVar) {
},
})
}

// additional GitRepo labels
for label, value := range gitrepo.Labels {
if label != "objectset.rio.cattle.io/hash" {
args = append(args, fmt.Sprintf("--label=%s=%s", label, value))
}
}

return append(args, gitrepo.Name), env
}

0 comments on commit 08414e2

Please sign in to comment.