Skip to content

Commit

Permalink
Fix issues with agent deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Sep 12, 2020
1 parent 4e9880a commit db171f7
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 29 deletions.
4 changes: 2 additions & 2 deletions modules/agent/pkg/deployer/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package deployer
import (
"sort"

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

fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/wrangler/pkg/apply"
"github.com/rancher/wrangler/pkg/name"
"github.com/rancher/wrangler/pkg/summary"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
Expand All @@ -23,6 +22,7 @@ func (m *Manager) getApply(bd *fleet.BundleDeployment, ns string) apply.Apply {
ns = m.defaultNamespace
}
return m.apply.
WithIgnorePreviousApplied().
WithSetID(name.SafeConcatName(m.labelPrefix, bd.Name)).
WithDefaultNamespace(ns)
}
Expand Down
9 changes: 8 additions & 1 deletion modules/agent/pkg/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,14 @@ func createClusterSecret(ctx context.Context, clusterID string, k8s corecontroll
},
}

return k8s.Secret().Create(updatedSecret)
secret, err := k8s.Secret().Create(updatedSecret)
if apierrors.IsAlreadyExists(err) {
if err = k8s.Secret().Delete(updatedSecret.Namespace, updatedSecret.Name, &metav1.DeleteOptions{}); err != nil {
return nil, err
}
secret, err = k8s.Secret().Create(updatedSecret)
}
return secret, err
}
}

Expand Down
33 changes: 11 additions & 22 deletions modules/cli/agentmanifest/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ var (
)

type Options struct {
CA []byte
Host string
NoCA bool
NoCheck bool
Labels map[string]string
ClientID string
CA []byte
Host string
NoCA bool
NoCheck bool
Labels map[string]string
ClientID string
Generation string
}

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

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

data, err := yaml.Export(objs...)
if err != nil {
Expand Down Expand Up @@ -162,6 +163,8 @@ func getKubeConfig(kubeConfig string, namespace string, token []byte, host strin
return "", err
}

customHost := len(host) > 0

host, doCheckHost, err := getHost(host, cfg)
if err != nil {
return "", err
Expand All @@ -175,7 +178,7 @@ func getKubeConfig(kubeConfig string, namespace string, token []byte, host strin

if noCA {
ca = nil
} else {
} else if !customHost {
ca, err = getCA(ca, cfg)
if err != nil {
return "", err
Expand Down Expand Up @@ -208,20 +211,6 @@ func getKubeConfig(kubeConfig string, namespace string, token []byte, host strin
return string(data), err
}

func getCluster(cfg clientcmdapi.Config) (*clientcmdapi.Cluster, error) {
ctx := cfg.Contexts[cfg.CurrentContext]
if ctx == nil {
return nil, fmt.Errorf("failed to find host for agent access, context not found")
}

cluster := cfg.Clusters[ctx.Cluster]
if cluster == nil {
return nil, fmt.Errorf("failed to find host for agent access, cluster not found")
}

return cluster, nil
}

func getHost(host string, cfg clientcmdapi.Config) (string, bool, error) {
if host != "" {
return host, false, nil
Expand Down
12 changes: 10 additions & 2 deletions pkg/agent/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package agent
import (
"github.com/rancher/fleet/pkg/basic"
"github.com/rancher/fleet/pkg/config"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/runtime"
)
Expand All @@ -11,7 +12,7 @@ const (
DefaultName = "fleet-agent"
)

func Manifest(namespace, image, pullPolicy string) []runtime.Object {
func Manifest(namespace, image, pullPolicy, generation string) []runtime.Object {
if image == "" {
image = config.DefaultAgentImage
}
Expand All @@ -26,8 +27,15 @@ func Manifest(namespace, image, pullPolicy string) []runtime.Object {
},
)

dep := basic.Deployment(namespace, DefaultName, image, pullPolicy, DefaultName)
dep.Spec.Template.Spec.Containers[0].Env = append(dep.Spec.Template.Spec.Containers[0].Env,
corev1.EnvVar{
Name: "GENERATION",
Value: generation,
})

objs := []runtime.Object{
basic.Deployment(namespace, DefaultName, image, pullPolicy, DefaultName),
dep,
sa,
}
objs = append(objs, clusterRole...)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/display/displaycontrollers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (h *handler) OnClusterChange(cluster *fleet.Cluster, status fleet.ClusterSt
}

status.Display.State = string(state)
if status.Agent.LastSeen.IsZero() && (status.AgentDeployed == nil || !*status.AgentDeployed) {
if status.Agent.LastSeen.IsZero() && status.AgentLastDeployed == nil {
status.Display.State = "ErrNoAgent"
}
return status, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/manageagent/manageagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (h *handler) getAgentBundle(ns string) ([]runtime.Object, error) {
return nil, nil
}

objs := agent.Manifest(h.systemNamespace, cfg.AgentImage, cfg.AgentImagePullPolicy)
objs := agent.Manifest(h.systemNamespace, cfg.AgentImage, cfg.AgentImagePullPolicy, "")
agentYAML, err := yaml.Export(objs...)
if err != nil {
return nil, err
Expand Down
30 changes: 30 additions & 0 deletions pkg/helmdeployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ func (h *helm) install(bundleID string, manifest *manifest.Manifest, chart *char
u.Timeout = timeout
u.DryRun = dryRun
u.PostRenderer = pr
if !dryRun {
logrus.Infof("Helm: Installing %s", bundleID)
}
return u.Run(chart, vals)
}

Expand All @@ -270,6 +273,9 @@ func (h *helm) install(bundleID string, manifest *manifest.Manifest, chart *char
u.Atomic = true
u.DryRun = dryRun
u.PostRenderer = pr
if !dryRun {
logrus.Infof("Helm: Upgrading %s", bundleID)
}
return u.Run(bundleID, chart, vals)
}

Expand Down Expand Up @@ -353,14 +359,38 @@ func (h *helm) delete(bundleID string, options fleet.BundleDeploymentOptions, dr
return err
}

if bundleID == "fleet-agent" {
// Never uninstall the fleet-agent, just "forget" it
return deleteHistory(cfg, bundleID)
}

u := action.NewUninstall(&cfg)
u.DryRun = dryRun
u.Timeout = timeout

if !dryRun {
logrus.Infof("Helm: Uninstalling %s", bundleID)
}
_, err = u.Run(bundleID)
return err
}

func deleteHistory(cfg action.Configuration, bundleID string) error {
releases, err := cfg.Releases.List(func(r *release.Release) bool {
return r.Name == bundleID && r.Chart.Metadata.Annotations[BundleIDAnnotation] == bundleID
})
if err != nil {
return err
}
for _, release := range releases {
logrus.Infof("Helm: Deleting release %s %d", release.Name, release.Version)
if _, err := cfg.Releases.Delete(release.Name, release.Version); err != nil {
return err
}
}
return nil
}

func releaseToResources(release *release.Release) (*deployer.Resources, error) {
var (
err error
Expand Down

0 comments on commit db171f7

Please sign in to comment.