Skip to content

Commit

Permalink
Switch sync timestamps to generation numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Oct 3, 2020
1 parent ef891e9 commit 66070b2
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 64 deletions.
9 changes: 3 additions & 6 deletions modules/agent/pkg/controllers/bundledeployment/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,10 @@ func (h *handler) Trigger(key string, bd *fleet.BundleDeployment) (*fleet.Bundle
}

func shouldRedeploy(bd *fleet.BundleDeployment) bool {
if bd.Spec.Options.ForceSyncBefore == nil {
return false
}
if bd.Status.ForceSync == nil {
if bd.Status.SyncGeneration == nil {
return true
}
return bd.Status.ForceSync.Before(bd.Spec.Options.ForceSyncBefore)
return *bd.Status.SyncGeneration != bd.Spec.Options.ForceSyncGeneration
}

func (h *handler) MonitorBundle(bd *fleet.BundleDeployment, status fleet.BundleDeploymentStatus) (fleet.BundleDeploymentStatus, error) {
Expand Down Expand Up @@ -140,7 +137,7 @@ func (h *handler) MonitorBundle(bd *fleet.BundleDeployment, status fleet.BundleD
}
}

status.ForceSync = bd.Spec.Options.ForceSyncBefore
status.SyncGeneration = &bd.Spec.Options.ForceSyncGeneration
if readyError != nil {
logrus.Errorf("bundle %s: %v", bd.Name, readyError)
}
Expand Down
5 changes: 2 additions & 3 deletions modules/cli/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"
"regexp"
"strings"
"time"

"github.com/rancher/fleet/modules/cli/pkg/client"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
Expand All @@ -37,7 +36,7 @@ type Options struct {
TargetNamespace string
Paused bool
Labels map[string]string
SyncBefore *time.Time
SyncGeneration int64
}

func globDirs(baseDir string) (result []string, err error) {
Expand Down Expand Up @@ -125,7 +124,7 @@ func readBundle(ctx context.Context, name, baseDir string, opts *Options) (*bund
TargetsFile: opts.TargetsFile,
TargetNamespace: opts.TargetNamespace,
Paused: opts.Paused,
SyncBefore: opts.SyncBefore,
SyncGeneration: opts.SyncGeneration,
})
}

Expand Down
9 changes: 2 additions & 7 deletions modules/cli/cmds/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Apply struct {
TargetsFile string `usage:"Addition source of targets and restrictions to be append"`
Compress bool `usage:"Force all resources to be compress" short:"c"`
ServiceAccount string `usage:"Service account to assign to bundle created" short:"a"`
SyncBefore string `usage:"Force sync all deployments before this RFC3339 time"`
SyncGeneration int `usage:"Generation number used to force sync the deployment"`
TargetNamespace string `usage:"Ensure this bundle goes to this target namespace"`
Paused bool `usage:"Create bundles in a paused state"`
}
Expand All @@ -42,11 +42,6 @@ func toTime(syncBefore string) (*time.Time, error) {
}

func (a *Apply) Run(cmd *cobra.Command, args []string) error {
syncBefore, err := toTime(a.SyncBefore)
if err != nil {
return err
}

labels := a.Label
if commit := os.Getenv("COMMIT"); commit != "" {
if labels == nil {
Expand All @@ -65,7 +60,7 @@ func (a *Apply) Run(cmd *cobra.Command, args []string) error {
TargetsFile: a.TargetsFile,
TargetNamespace: a.TargetNamespace,
Paused: a.Paused,
SyncBefore: syncBefore,
SyncGeneration: int64(a.SyncGeneration),
}

if a.File == "-" {
Expand Down
18 changes: 9 additions & 9 deletions pkg/apis/fleet.cattle.io/v1alpha1/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,14 @@ type BundleDeployment struct {
}

type BundleDeploymentOptions struct {
DefaultNamespace string `json:"defaultNamespace,omitempty"`
TargetNamespace string `json:"namespace,omitempty"`
Kustomize *KustomizeOptions `json:"kustomize,omitempty"`
Helm *HelmOptions `json:"helm,omitempty"`
ServiceAccount string `json:"serviceAccount,omitempty"`
ForceSyncBefore *metav1.Time `json:"forceSyncBefore,omitempty"`
YAML *YAMLOptions `json:"yaml,omitempty"`
Diff *DiffOptions `json:"diff,omitempty"`
DefaultNamespace string `json:"defaultNamespace,omitempty"`
TargetNamespace string `json:"namespace,omitempty"`
Kustomize *KustomizeOptions `json:"kustomize,omitempty"`
Helm *HelmOptions `json:"helm,omitempty"`
ServiceAccount string `json:"serviceAccount,omitempty"`
ForceSyncGeneration int64 `json:"forceSyncGeneration,omitempty"`
YAML *YAMLOptions `json:"yaml,omitempty"`
Diff *DiffOptions `json:"diff,omitempty"`
}

type DiffOptions struct {
Expand Down Expand Up @@ -229,7 +229,7 @@ type BundleDeploymentStatus struct {
NonReadyStatus []NonReadyStatus `json:"nonReadyStatus,omitempty"`
ModifiedStatus []ModifiedStatus `json:"modifiedStatus,omitempty"`
Display BundleDeploymentDisplay `json:"display,omitempty"`
ForceSync *metav1.Time `json:"forceSync,omitempty"`
SyncGeneration *int64 `json:"syncGeneration,omitempty"`
}

type BundleDeploymentDisplay struct {
Expand Down
7 changes: 2 additions & 5 deletions pkg/apis/fleet.cattle.io/v1alpha1/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,8 @@ type GitRepoSpec struct {
// PollingInterval is how often to check git for new updates
PollingInterval *metav1.Duration `json:"pollingInterval,omitempty"`

// All non-ready deployments before this time will be resynced
ForceSyncBefore *metav1.Time `json:"forceSyncBefore,omitempty"`

// ForceUpdate is a timestamp if set to Now() will cause the git repo to be checked again
ForceUpdate *metav1.Time `json:"forceUpdate,omitempty"`
// Increment this number to force a redeployment of contents from Git
ForceSyncGeneration int64 `json:"forceSyncGeneration,omitempty"`
}

type GitTarget struct {
Expand Down
10 changes: 5 additions & 5 deletions pkg/apis/fleet.cattle.io/v1alpha1/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ type Cluster struct {
}

type ClusterSpec struct {
Paused bool `json:"paused,omitempty"`
ClientID string `json:"clientID,omitempty"`
KubeConfigSecret string `json:"kubeConfigSecret,omitempty"`
ForceUpdateAgent *metav1.Time `json:"forceUpdateAgent,omitempty"`
Paused bool `json:"paused,omitempty"`
ClientID string `json:"clientID,omitempty"`
KubeConfigSecret string `json:"kubeConfigSecret,omitempty"`
RedeployAgentGeneration int64 `json:"redeployAgentGeneration,omitempty"`
}

type ClusterStatus struct {
Expand All @@ -74,7 +74,7 @@ type ClusterStatus struct {
ReadyGitRepos int `json:"readyGitRepos"`
DesiredReadyGitRepos int `json:"desiredReadyGitRepos"`

AgentLastDeployed *metav1.Time `json:"agentLastDeployed,omitempty"`
AgentDeployedGeneration *int64 `json:"agentDeployedGeneration,omitempty"`

Display ClusterDisplay `json:"display,omitempty"`
Agent AgentStatus `json:"agent,omitempty"`
Expand Down
7 changes: 2 additions & 5 deletions pkg/bundle/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"io/ioutil"
"os"
"path/filepath"
"time"

fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -23,7 +22,7 @@ type Options struct {
TargetsFile string
TargetNamespace string
Paused bool
SyncBefore *time.Time
SyncGeneration int64
}

func Open(ctx context.Context, name, baseDir, file string, opts *Options) (*Bundle, error) {
Expand Down Expand Up @@ -151,9 +150,7 @@ func read(ctx context.Context, name, baseDir string, bundleSpecReader io.Reader,
def.Spec.ServiceAccount = opts.ServiceAccount
}

if opts.SyncBefore != nil {
def.Spec.ForceSyncBefore = &metav1.Time{Time: *opts.SyncBefore}
}
def.Spec.ForceSyncGeneration = opts.SyncGeneration

def, err = appendTargets(def, opts.TargetsFile)
if err != nil {
Expand Down
14 changes: 3 additions & 11 deletions pkg/controllers/cluster/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,10 @@ func RegisterImport(
}

func agentDeployed(cluster *fleet.Cluster) bool {
if cluster.Status.AgentLastDeployed == nil {
if cluster.Status.AgentDeployedGeneration == nil {
return false
}
if cluster.Spec.ForceUpdateAgent == nil {
return true
}
return !cluster.Status.AgentLastDeployed.Before(cluster.Spec.ForceUpdateAgent)
return *cluster.Status.AgentDeployedGeneration != cluster.Spec.RedeployAgentGeneration
}

func (i *importHandler) OnChange(key string, cluster *fleet.Cluster) (_ *fleet.Cluster, err error) {
Expand Down Expand Up @@ -184,11 +181,6 @@ func (i *importHandler) importCluster(cluster *fleet.Cluster, status fleet.Clust
return status, err
}

if cluster.Spec.ForceUpdateAgent != nil {
status.AgentLastDeployed = cluster.Spec.ForceUpdateAgent
} else {
now := metav1.Now()
status.AgentLastDeployed = &now
}
status.AgentDeployedGeneration = &cluster.Spec.RedeployAgentGeneration
return status, nil
}
3 changes: 0 additions & 3 deletions pkg/controllers/display/displaycontrollers.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ func (h *handler) OnClusterChange(cluster *fleet.Cluster, status fleet.ClusterSt
}

status.Display.State = string(state)
if status.Agent.LastSeen.IsZero() && status.AgentLastDeployed == nil {
status.Display.State = "ErrNoAgent"
}
return status, nil
}

Expand Down
11 changes: 3 additions & 8 deletions pkg/controllers/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,6 @@ func (h *handler) OnChange(gitrepo *fleet.GitRepo, status fleet.GitRepoStatus) (
syncSeconds = int(gitrepo.Spec.PollingInterval.Duration / time.Second)
}

syncBefore := ""
if gitrepo.Spec.ForceSyncBefore != nil {
syncBefore = gitrepo.Spec.ForceSyncBefore.UTC().Format(time.RFC3339)
}

saName := name.SafeConcatName("git", gitrepo.Name)

bundleErrorState := ""
Expand Down Expand Up @@ -373,8 +368,8 @@ func (h *handler) OnChange(gitrepo *fleet.GitRepo, status fleet.GitRepoStatus) (
Namespace: gitrepo.Namespace,
},
Spec: gitjob.GitJobSpec{
SyncInterval: syncSeconds,
ForceUpdate: gitrepo.Spec.ForceUpdate,
SyncInterval: syncSeconds,
ForceUpdateGeneration: gitrepo.Spec.ForceSyncGeneration,
Git: gitjob.GitInfo{
Credential: gitjob.Credential{
ClientSecretName: gitrepo.Spec.ClientSecretName,
Expand Down Expand Up @@ -418,7 +413,7 @@ func (h *handler) OnChange(gitrepo *fleet.GitRepo, status fleet.GitRepoStatus) (
"--label=" + fleet.RepoLabel + "=" + gitrepo.Name,
"--namespace", gitrepo.Namespace,
"--service-account", gitrepo.Spec.ServiceAccount,
"--sync-before", syncBefore,
fmt.Sprintf("--sync-generation=%d", gitrepo.Spec.ForceSyncGeneration),
fmt.Sprintf("--paused=%v", gitrepo.Spec.Paused),
"--target-namespace", gitrepo.Spec.TargetNamespace,
gitrepo.Name,
Expand Down
4 changes: 2 additions & 2 deletions pkg/options/calculate.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ func merge(base, next fleet.BundleDeploymentOptions) fleet.BundleDeploymentOptio
}
base.YAML.Overlays = append(base.YAML.Overlays, next.YAML.Overlays...)
}
if next.ForceSyncBefore != nil {
base.ForceSyncBefore = next.ForceSyncBefore
if next.ForceSyncGeneration > 0 {
base.ForceSyncGeneration = next.ForceSyncGeneration
}
return base
}

0 comments on commit 66070b2

Please sign in to comment.