Skip to content

Commit

Permalink
Add install command
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Mar 30, 2020
1 parent 96b2c6a commit b32e77c
Show file tree
Hide file tree
Showing 28 changed files with 737 additions and 361 deletions.
3 changes: 2 additions & 1 deletion cmd/fleetmanager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

type FleetManager struct {
Kubeconfig string `usage:"Kubeconfig file"`
Namespace string `usage:"namespace to watch" default:"fleet-system"`
OutputCRDs bool `usage:"Print CRD definitions to stdout and exit"`
}

Expand All @@ -19,7 +20,7 @@ func (f *FleetManager) Run(cmd *cobra.Command, args []string) error {
return fleetmanager.OutputCRDs(os.Stdout)
}

if err := fleetmanager.Start(cmd.Context(), f.Kubeconfig); err != nil {
if err := fleetmanager.Start(cmd.Context(), f.Namespace, f.Kubeconfig); err != nil {
return err
}

Expand Down
32 changes: 23 additions & 9 deletions modules/agent/pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (
"strings"
"time"

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

fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"

"github.com/rancher/fleet/modules/agent/pkg/deployer"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/fleet/pkg/kustomize"
"github.com/rancher/fleet/pkg/manifest"
"github.com/rancher/fleet/pkg/render"
"github.com/rancher/wrangler/pkg/apply"
"github.com/rancher/wrangler/pkg/kv"
"github.com/rancher/wrangler/pkg/name"
"github.com/rancher/wrangler/pkg/yaml"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -52,6 +51,8 @@ func mergeMaps(base, other map[string]string) map[string]string {

type postRender struct {
bundleID string
manifest *manifest.Manifest
opts fleet.BundleDeploymentOptions
}

func (p *postRender) Run(renderedManifests *bytes.Buffer) (modifiedManifests *bytes.Buffer, err error) {
Expand All @@ -60,6 +61,14 @@ func (p *postRender) Run(renderedManifests *bytes.Buffer) (modifiedManifests *by
return nil, err
}

newObjs, processed, err := kustomize.Process(p.manifest, renderedManifests.Bytes(), p.opts.KustomizeDir)
if err != nil {
return nil, err
}
if processed {
objs = newObjs
}

labels, annotations, err := apply.GetLabelsAndAnnotations(name.SafeConcatName("fleet", p.bundleID), nil)
if err != nil {
return nil, err
Expand Down Expand Up @@ -89,6 +98,11 @@ func (h *helm) Deploy(bundleID string, manifest *manifest.Manifest, options flee
return nil, err
}

if chart.Metadata.Annotations == nil {
chart.Metadata.Annotations = map[string]string{}
}
chart.Metadata.Annotations["bundleID"] = bundleID

if _, err := h.install(bundleID, chart, options, true); err != nil {
return nil, err
}
Expand Down Expand Up @@ -198,7 +212,7 @@ func (h *helm) ListDeployments() ([]string, error) {
)

for _, release := range releases {
d := release.Chart.Metadata.Annotations["deploymentID"]
d := release.Chart.Metadata.Annotations["bundleID"]
if d != "" && !seen[d] {
result = append(result, d)
seen[d] = true
Expand Down Expand Up @@ -228,18 +242,18 @@ func (h *helm) Resources(deploymentID, resourcesID string) (*deployer.Resources,
return &deployer.Resources{}, nil
}

func (h *helm) Delete(deploymentID string) error {
return h.delete(deploymentID, fleet.BundleDeploymentOptions{}, false)
func (h *helm) Delete(bundleID string) error {
return h.delete(bundleID, fleet.BundleDeploymentOptions{}, false)
}

func (h *helm) delete(deploymentID string, options fleet.BundleDeploymentOptions, dryRun bool) error {
func (h *helm) delete(bundleID string, options fleet.BundleDeploymentOptions, dryRun bool) error {
_, timeout, _ := getOpts(options)

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

_, err := u.Run(deploymentID)
_, err := u.Run(bundleID)
return err
}

Expand Down
2 changes: 1 addition & 1 deletion modules/agent/pkg/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func createClusterSecret(ctx context.Context, k8s corev1.Interface, secret *v1.S
return nil, err
}

cfg, err := config.Lookup(ctx, secret.Namespace, config.AgentName, k8s.ConfigMap())
cfg, err := config.Lookup(ctx, secret.Namespace, config.AgentConfigName, k8s.ConfigMap())
if err != nil {
return nil, err
}
Expand Down
49 changes: 49 additions & 0 deletions modules/cli/agentconfig/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package agentconfig

import (
"io"

"github.com/rancher/fleet/modules/cli/pkg/client"

"github.com/rancher/fleet/pkg/config"

"k8s.io/apimachinery/pkg/runtime"

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

type Options struct {
Labels map[string]string
}

func AgentConfig(output io.Writer, cg *client.Getter, opts *Options) error {
if opts == nil {
opts = &Options{}
}

objs, err := configMap(cg.Namespace, opts.Labels)
if err != nil {
return err
}

data, err := yaml.Export(objs...)
if err != nil {
return err
}

_, err = output.Write(data)
return err
}

func configMap(namespace string, clusterLabels map[string]string) ([]runtime.Object, error) {
cm, err := config.ToConfigMap(namespace, config.AgentConfigName, &config.Config{
Labels: clusterLabels,
})
if err != nil {
return nil, err
}
cm.Name = "fleet-agent"
return []runtime.Object{
cm,
}, nil
}
45 changes: 22 additions & 23 deletions modules/cli/agentmanifest/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,28 @@ import (
"io/ioutil"
"time"

fleetcontrollers "github.com/rancher/fleet/pkg/generated/controllers/fleet.cattle.io/v1alpha1"

"github.com/pkg/errors"
"github.com/rancher/fleet/modules/cli/pkg/client"
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/wrangler/pkg/kubeconfig"
"github.com/rancher/wrangler/pkg/yaml"
coreV1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

type Options struct {
TTL time.Duration
CA []byte
Host string
Labels map[string]string
ConfigOnly bool
TTL time.Duration
CA []byte
Host string
}

func AgentManifest(ctx context.Context, clusterGroupName string, cg *client.Getter, output io.Writer, opts *Options) error {
func AgentManifest(ctx context.Context, managerNamespace, clusterGroupName string, cg *client.Getter, output io.Writer, opts *Options) error {
if opts == nil {
opts = &Options{}
}
Expand All @@ -40,7 +38,7 @@ func AgentManifest(ctx context.Context, clusterGroupName string, cg *client.Gett
return err
}

cfg, err := config.Lookup(ctx, config.Namespace, config.Name, client.Core.ConfigMap())
cfg, err := config.Lookup(ctx, managerNamespace, config.ManagerConfigName, client.Core.ConfigMap())
if err != nil {
return err
}
Expand All @@ -50,25 +48,18 @@ func AgentManifest(ctx context.Context, clusterGroupName string, cg *client.Gett
return err
}

objs, err := configMap(opts.Labels)
token, err := getToken(ctx, clusterGroup, opts.TTL, client)
if err != nil {
return err
}

if !opts.ConfigOnly {
token, err := getToken(ctx, clusterGroup, opts.TTL, client)
if err != nil {
return err
}

kubeConfig, err := getKubeConfig(cg.Kubeconfig, clusterGroup.Status.Namespace, token, opts.Host, opts.CA)
if err != nil {
return err
}

objs = append(objects(kubeConfig, cfg.AgentImage), objs...)
kubeConfig, err := getKubeConfig(cg.Kubeconfig, clusterGroup.Status.Namespace, token, opts.Host, opts.CA)
if err != nil {
return err
}

objs := objects(managerNamespace, kubeConfig, cfg.AgentImage)

data, err := yaml.Export(objs...)
if err != nil {
return err
Expand All @@ -82,8 +73,16 @@ func getClusterGroup(ctx context.Context, clusterGroupName string, client *clien
timeout := time.After(5 * time.Second)
for {
clusterGroup, err := client.Fleet.ClusterGroup().Get(client.Namespace, clusterGroupName, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
clusterGroup, err = client.Fleet.ClusterGroup().Create(&fleet.ClusterGroup{
ObjectMeta: metav1.ObjectMeta{
Namespace: client.Namespace,
Name: clusterGroupName,
},
})
}
if err != nil {
return nil, errors.Wrapf(err, "invalid cluster group, namespace=%s", client.Namespace)
return nil, errors.Wrapf(err, "invalid cluster group %s/%s", client.Namespace, clusterGroupName)
}

if clusterGroup.Status.Namespace != "" {
Expand Down
24 changes: 5 additions & 19 deletions modules/cli/agentmanifest/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,20 @@ import (
"github.com/rancher/fleet/pkg/agent"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/fleet/pkg/config"
"github.com/rancher/fleet/pkg/version"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

func configMap(clusterLabels map[string]string) ([]runtime.Object, error) {
cm, err := config.ToConfigMap(config.Namespace, config.AgentName, &config.Config{
Labels: clusterLabels,
})
if err != nil {
return nil, err
}
cm.Name = "fleet-agent"
return []runtime.Object{
cm,
}, nil
}

func objects(kubeconfig, image string) []runtime.Object {
func objects(namespace, kubeconfig, image string) []runtime.Object {
if image == "" {
image = "rancher/fleet-agent:" + version.Version
image = config.DefaultAgentImage
}

objs := []runtime.Object{
&v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: config.Namespace,
Name: namespace,
Annotations: map[string]string{
fleet.ManagedAnnotation: "true",
},
Expand All @@ -40,7 +26,7 @@ func objects(kubeconfig, image string) []runtime.Object {
&v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: agent.DefaultName,
Namespace: config.Namespace,
Namespace: namespace,
Annotations: map[string]string{
fleet.BootstrapToken: "true",
},
Expand All @@ -51,6 +37,6 @@ func objects(kubeconfig, image string) []runtime.Object {
},
}

objs = append(objs, agent.Manifest(image)...)
objs = append(objs, agent.Manifest(namespace, image)...)
return objs
}
54 changes: 0 additions & 54 deletions modules/cli/cmds/agent.go

This file was deleted.

28 changes: 28 additions & 0 deletions modules/cli/cmds/agentconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmds

import (
"github.com/rancher/fleet/modules/cli/agentconfig"
"github.com/rancher/fleet/modules/cli/pkg/command"
"github.com/rancher/fleet/modules/cli/pkg/writer"
"github.com/spf13/cobra"
)

func NewAgentConfig() *cobra.Command {
return command.Command(&AgentConfig{}, cobra.Command{
Short: "Generate cluster specific agent config",
})
}

type AgentConfig struct {
OutputArgs

Labels map[string]string `usage:"Labels to apply to the new cluster on register" short:"l"`
}

func (a *AgentConfig) Run(cmd *cobra.Command, args []string) error {
opts := &agentconfig.Options{
Labels: a.Labels,
}

return agentconfig.AgentConfig(writer.New(a.Output), Client, opts)
}
Loading

0 comments on commit b32e77c

Please sign in to comment.