Skip to content

Commit

Permalink
add env vars to agent manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
kinarashah authored and Daishan committed Apr 7, 2021
1 parent 3fa1d90 commit 28ebe4f
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 59 deletions.
12 changes: 12 additions & 0 deletions charts/fleet-crd/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ spec:
nullable: true
type: object
type: object
clusterName:
nullable: true
type: string
clusterSelector:
nullable: true
properties:
Expand Down Expand Up @@ -252,6 +255,9 @@ spec:
nullable: true
type: object
type: object
clusterName:
nullable: true
type: string
clusterSelector:
nullable: true
properties:
Expand Down Expand Up @@ -320,6 +326,9 @@ spec:
nullable: true
type: object
type: object
clusterName:
nullable: true
type: string
clusterSelector:
nullable: true
properties:
Expand Down Expand Up @@ -1890,6 +1899,9 @@ spec:
nullable: true
type: object
type: object
clusterName:
nullable: true
type: string
clusterSelector:
nullable: true
properties:
Expand Down
4 changes: 3 additions & 1 deletion modules/cli/agentmanifest/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
fleetcontrollers "github.com/rancher/fleet/pkg/generated/controllers/fleet.cattle.io/v1alpha1"
"github.com/rancher/wrangler/pkg/kubeconfig"
"github.com/rancher/wrangler/pkg/yaml"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
Expand All @@ -40,6 +41,7 @@ type Options struct {
ClientID string
Generation string
CheckinInterval string
AgentEnvVars []v1.EnvVar
}

func AgentToken(ctx context.Context, controllerNamespace string, client *client.Client, tokenName string, opts *Options) ([]runtime.Object, error) {
Expand Down Expand Up @@ -123,7 +125,7 @@ func AgentManifest(ctx context.Context, systemNamespace, controllerNamespace str
return err
}

objs = append(objs, agent.Manifest(controllerNamespace, cfg.AgentImage, cfg.AgentImagePullPolicy, opts.Generation, opts.CheckinInterval)...)
objs = append(objs, agent.Manifest(controllerNamespace, cfg.AgentImage, cfg.AgentImagePullPolicy, opts.Generation, opts.CheckinInterval, opts.AgentEnvVars)...)

data, err := yaml.Export(objs...)
if err != nil {
Expand Down
12 changes: 7 additions & 5 deletions modules/cli/cmds/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ func NewTest() *cobra.Command {

type Test struct {
BundleInputArgs
Quiet bool `usage:"Just print the match and don't print the resources" short:"q"`
Group string `usage:"Cluster group to match against" short:"g"`
Label map[string]string `usage:"Cluster labels to match against" short:"l"`
GroupLabel map[string]string `usage:"Cluster group labels to match against" short:"L"`
Target string `usage:"Explicit target to match" short:"t"`
Quiet bool `usage:"Just print the match and don't print the resources" short:"q"`
Group string `usage:"Cluster group to match against" short:"g"`
ClusterName string `usage:"Cluster name to match against" short:"n"`
Label map[string]string `usage:"Cluster labels to match against" short:"l"`
GroupLabel map[string]string `usage:"Cluster group labels to match against" short:"L"`
Target string `usage:"Explicit target to match" short:"t"`
}

func (m *Test) Run(cmd *cobra.Command, args []string) error {
Expand All @@ -35,6 +36,7 @@ func (m *Test) Run(cmd *cobra.Command, args []string) error {
BaseDir: baseDir,
BundleSpec: m.File,
BundleFile: m.BundleFile,
ClusterName: m.ClusterName,
ClusterGroup: m.Group,
ClusterLabels: m.Label,
ClusterGroupLabels: m.GroupLabel,
Expand Down
3 changes: 2 additions & 1 deletion modules/cli/match/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Options struct {
BaseDir string
BundleSpec string
BundleFile string
ClusterName string
ClusterGroup string
ClusterLabels map[string]string
ClusterGroupLabels map[string]string
Expand Down Expand Up @@ -61,7 +62,7 @@ func Match(ctx context.Context, opts *Options) error {
}

if opts.Target == "" {
m := b.Match(map[string]map[string]string{
m := b.Match(opts.ClusterName, map[string]map[string]string{
opts.ClusterGroup: opts.ClusterGroupLabels,
}, opts.ClusterLabels)
return printMatch(b, m, opts.Output)
Expand Down
5 changes: 4 additions & 1 deletion pkg/agent/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
DefaultName = "fleet-agent"
)

func Manifest(namespace, image, pullPolicy, generation, checkInInterval string) []runtime.Object {
func Manifest(namespace, image, pullPolicy, generation, checkInInterval string, agentEnvVars []corev1.EnvVar) []runtime.Object {
if image == "" {
image = config.DefaultAgentImage
}
Expand Down Expand Up @@ -42,6 +42,9 @@ func Manifest(namespace, image, pullPolicy, generation, checkInInterval string)
Name: "GENERATION",
Value: generation,
})
if agentEnvVars != nil {
dep.Spec.Template.Spec.Containers[0].Env = append(dep.Spec.Template.Spec.Containers[0].Env, agentEnvVars...)
}
dep.Spec.Template.Spec.Affinity = &corev1.Affinity{
NodeAffinity: &corev1.NodeAffinity{
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.PreferredSchedulingTerm{
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/fleet.cattle.io/v1alpha1/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ type RolloutStrategy struct {
type Partition struct {
Name string `json:"name,omitempty"`
MaxUnavailable *intstr.IntOrString `json:"maxUnavailable,omitempty"`
ClusterName string `json:"clusterName,omitempty"`
ClusterSelector *metav1.LabelSelector `json:"clusterSelector,omitempty"`
ClusterGroup string `json:"clusterGroup,omitempty"`
ClusterGroupSelector *metav1.LabelSelector `json:"clusterGroupSelector,omitempty"`
}

type BundleTargetRestriction struct {
Name string `json:"name,omitempty"`
ClusterName string `json:"clusterName,omitempty"`
ClusterSelector *metav1.LabelSelector `json:"clusterSelector,omitempty"`
ClusterGroup string `json:"clusterGroup,omitempty"`
ClusterGroupSelector *metav1.LabelSelector `json:"clusterGroupSelector,omitempty"`
Expand All @@ -96,6 +98,7 @@ type BundleTargetRestriction struct {
type BundleTarget struct {
BundleDeploymentOptions
Name string `json:"name,omitempty"`
ClusterName string `json:"clusterName,omitempty"`
ClusterSelector *metav1.LabelSelector `json:"clusterSelector,omitempty"`
ClusterGroup string `json:"clusterGroup,omitempty"`
ClusterGroupSelector *metav1.LabelSelector `json:"clusterGroupSelector,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/fleet.cattle.io/v1alpha1/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type GitRepoSpec struct {

type GitTarget struct {
Name string `json:"name,omitempty"`
ClusterName string `json:"clusterName,omitempty"`
ClusterSelector *metav1.LabelSelector `json:"clusterSelector,omitempty"`
ClusterGroup string `json:"clusterGroup,omitempty"`
ClusterGroupSelector *metav1.LabelSelector `json:"clusterGroupSelector,omitempty"`
Expand Down
20 changes: 10 additions & 10 deletions pkg/bundle/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func (a *Bundle) MatchForTarget(name string) *Match {
return nil
}

func (a *Bundle) Match(clusterGroups map[string]map[string]string, clusterLabels map[string]string) *Match {
func (a *Bundle) Match(clusterName string, clusterGroups map[string]map[string]string, clusterLabels map[string]string) *Match {
for clusterGroup, clusterGroupLabels := range clusterGroups {
if m := a.matcher.Match(clusterGroup, clusterGroupLabels, clusterLabels); m != nil {
if m := a.matcher.Match(clusterName, clusterGroup, clusterGroupLabels, clusterLabels); m != nil {
return m
}
}
if len(clusterGroups) == 0 {
return a.matcher.Match("", nil, clusterLabels)
return a.matcher.Match(clusterName, "", nil, clusterLabels)
}
return nil
}
Expand All @@ -51,7 +51,7 @@ func (a *Bundle) initMatcher() error {
)

for i, target := range a.Definition.Spec.Targets {
clusterMatcher, err := match.NewClusterMatcher(target.ClusterGroup, target.ClusterGroupSelector, target.ClusterSelector)
clusterMatcher, err := match.NewClusterMatcher(target.ClusterName, target.ClusterGroup, target.ClusterGroupSelector, target.ClusterSelector)
if err != nil {
return err
}
Expand All @@ -67,7 +67,7 @@ func (a *Bundle) initMatcher() error {
}

for _, target := range a.Definition.Spec.TargetRestrictions {
clusterMatcher, err := match.NewClusterMatcher(target.ClusterGroup, target.ClusterGroupSelector, target.ClusterSelector)
clusterMatcher, err := match.NewClusterMatcher(target.ClusterName, target.ClusterGroup, target.ClusterGroupSelector, target.ClusterSelector)
if err != nil {
return err
}
Expand All @@ -78,27 +78,27 @@ func (a *Bundle) initMatcher() error {
return nil
}

func (m *matcher) isRestricted(clusterGroup string, clusterGroupLabels, clusterLabels map[string]string) bool {
func (m *matcher) isRestricted(clusterName, clusterGroup string, clusterGroupLabels, clusterLabels map[string]string) bool {
if len(m.restrictions) == 0 {
return false
}

for _, restriction := range m.restrictions {
if restriction.Match(clusterGroup, clusterGroupLabels, clusterLabels) {
if restriction.Match(clusterName, clusterGroup, clusterGroupLabels, clusterLabels) {
return false
}
}

return true
}

func (m *matcher) Match(clusterGroup string, clusterGroupLabels, clusterLabels map[string]string) *Match {
if m.isRestricted(clusterGroup, clusterGroupLabels, clusterLabels) {
func (m *matcher) Match(clusterName, clusterGroup string, clusterGroupLabels, clusterLabels map[string]string) *Match {
if m.isRestricted(clusterName, clusterGroup, clusterGroupLabels, clusterLabels) {
return nil
}

for _, targetMatch := range m.matches {
if targetMatch.criteria.Match(clusterGroup, clusterGroupLabels, clusterLabels) {
if targetMatch.criteria.Match(clusterName, clusterGroup, clusterGroupLabels, clusterLabels) {
return targetMatch.targetBundle
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/controllers/cluster/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func (i *importHandler) importCluster(cluster *fleet.Cluster, status fleet.Clust
CA: apiServerCA,
Host: apiServerURL,
ClientID: cluster.Spec.ClientID,
AgentEnvVars: cluster.Spec.AgentEnvVars,
CheckinInterval: cfg.AgentCheckinInternal.Duration.String(),
Generation: string(cluster.UID) + "-" + strconv.FormatInt(cluster.Generation, 10),
})
Expand Down
2 changes: 2 additions & 0 deletions pkg/controllers/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ func (h *handler) getConfig(repo *fleet.GitRepo) (*corev1.ConfigMap, error) {
for _, target := range targetsOrDefault(repo.Spec.Targets) {
spec.Targets = append(spec.Targets, fleet.BundleTarget{
Name: target.Name,
ClusterName: target.ClusterName,
ClusterSelector: target.ClusterSelector,
ClusterGroup: target.ClusterGroup,
ClusterGroupSelector: target.ClusterGroupSelector,
})
spec.TargetRestrictions = append(spec.TargetRestrictions, fleet.BundleTargetRestriction{
Name: target.Name,
ClusterName: target.ClusterName,
ClusterSelector: target.ClusterSelector,
ClusterGroup: target.ClusterGroup,
ClusterGroupSelector: target.ClusterGroupSelector,
Expand Down
65 changes: 35 additions & 30 deletions pkg/controllers/manageagent/manageagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
fleetcontrollers "github.com/rancher/fleet/pkg/generated/controllers/fleet.cattle.io/v1alpha1"
"github.com/rancher/wrangler/pkg/apply"
corecontrollers "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
"github.com/rancher/wrangler/pkg/name"
"github.com/rancher/wrangler/pkg/relatedresource"
"github.com/rancher/wrangler/pkg/yaml"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -71,9 +72,14 @@ func (h *handler) OnNamespace(key string, namespace *corev1.Namespace) (*corev1.
return namespace, nil
}

objs, err := h.getAgentBundle(namespace.Name)
if err != nil {
return nil, err
var objs []runtime.Object

for _, cluster := range clusters {
bundle, err := h.getAgentBundle(namespace.Name, cluster)
if err != nil {
return nil, err
}
objs = append(objs, bundle)
}

return namespace, h.apply.
Expand All @@ -83,48 +89,47 @@ func (h *handler) OnNamespace(key string, namespace *corev1.Namespace) (*corev1.
ApplyObjects(objs...)
}

func (h *handler) getAgentBundle(ns string) ([]runtime.Object, error) {
func (h *handler) getAgentBundle(ns string, cluster *fleet.Cluster) (runtime.Object, error) {
cfg := config.Get()
if cfg.ManageAgent != nil && !*cfg.ManageAgent {
return nil, nil
}

objs := agent.Manifest(h.systemNamespace, cfg.AgentImage, cfg.AgentImagePullPolicy, "bundle", cfg.AgentCheckinInternal.Duration.String())
objs := agent.Manifest(h.systemNamespace, cfg.AgentImage, cfg.AgentImagePullPolicy, "bundle", cfg.AgentCheckinInternal.Duration.String(), cluster.Spec.AgentEnvVars)
agentYAML, err := yaml.Export(objs...)
if err != nil {
return nil, err
}

return []runtime.Object{
&fleet.Bundle{
ObjectMeta: metav1.ObjectMeta{
Name: agentBundleName,
Namespace: ns,
},
Spec: fleet.BundleSpec{
BundleDeploymentOptions: fleet.BundleDeploymentOptions{
DefaultNamespace: h.systemNamespace,
Helm: &fleet.HelmOptions{
TakeOwnership: true,
},
return &fleet.Bundle{
ObjectMeta: metav1.ObjectMeta{
Name: name.SafeConcatName(agentBundleName, cluster.Name),
Namespace: ns,
},
Spec: fleet.BundleSpec{
BundleDeploymentOptions: fleet.BundleDeploymentOptions{
DefaultNamespace: h.systemNamespace,
Helm: &fleet.HelmOptions{
TakeOwnership: true,
},
Resources: []fleet.BundleResource{
{
Name: "agent.yaml",
Content: string(agentYAML),
},
},
Resources: []fleet.BundleResource{
{
Name: "agent.yaml",
Content: string(agentYAML),
},
Targets: []fleet.BundleTarget{
{
ClusterSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "fleet.cattle.io/non-managed-agent",
Operator: metav1.LabelSelectorOpDoesNotExist,
},
},
Targets: []fleet.BundleTarget{
{
ClusterSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Key: "fleet.cattle.io/non-managed-agent",
Operator: metav1.LabelSelectorOpDoesNotExist,
},
},
},
ClusterName: cluster.Name,
},
},
},
Expand Down
Loading

0 comments on commit 28ebe4f

Please sign in to comment.