Skip to content

Commit

Permalink
Merge pull request #250 from projectsyn/feat/adopt-repo
Browse files Browse the repository at this point in the history
Make git repo creation behaviour configurable
  • Loading branch information
glrf authored Sep 30, 2022
2 parents f208f57 + f858235 commit 6654916
Show file tree
Hide file tree
Showing 18 changed files with 284 additions and 40 deletions.
10 changes: 10 additions & 0 deletions api/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ type ClusterSpec struct {
// Archive: will archive the external resources, if it supports that
// +kubebuilder:validation:Enum=Delete;Retain;Archive
DeletionPolicy DeletionPolicy `json:"deletionPolicy,omitempty"`
// CreationPolicy defines how the external resources should be treated upon CR creation.
// Create: will only create a new external resource and will not manage already existing resources
// Adopt: will create a new external resource or will adopt and manage an already existing resource
// +kubebuilder:validation:Enum=Create;Adopt
CreationPolicy CreationPolicy `json:"creationPolicy,omitempty"`
}

// BootstrapToken this key is used only once for Steward to register.
Expand Down Expand Up @@ -99,6 +104,11 @@ func (c *Cluster) GetDeletionPolicy() DeletionPolicy {
return c.Spec.DeletionPolicy
}

// GetCreationPolicy returns the object's creation policy
func (c *Cluster) GetCreationPolicy() CreationPolicy {
return c.Spec.CreationPolicy
}

// GetDisplayName returns the display name of the object
func (c *Cluster) GetDisplayName() string {
return c.Spec.DisplayName
Expand Down
15 changes: 15 additions & 0 deletions api/v1alpha1/gitrepo_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ const (
ArchivePolicy DeletionPolicy = "Archive"
DeletePolicy DeletionPolicy = "Delete"
RetainPolicy DeletionPolicy = "Retain"
CreatePolicy CreationPolicy = "Create"
AdoptPolicy CreationPolicy = "Adopt"
)

// GitPhase is the enum for the git phase status
Expand All @@ -45,6 +47,9 @@ type RepoType string
// DeletionPolicy defines the type deletion policy
type DeletionPolicy string

// CreationPolicy defines the type creation policy
type CreationPolicy string

// GitRepoSpec defines the desired state of GitRepo
type GitRepoSpec struct {
GitRepoTemplate `json:",inline"`
Expand Down Expand Up @@ -77,6 +82,11 @@ type GitRepoTemplate struct {
// Archive: will archive the external resources, if it supports that
// +kubebuilder:validation:Enum=Delete;Retain;Archive
DeletionPolicy DeletionPolicy `json:"deletionPolicy,omitempty"`
// CreationPolicy defines how the external resources should be treated upon CR creation.
// Create: will only create a new external resource and will not manage already existing resources
// Adopt: will create a new external resource or will adopt and manage an already existing resource
// +kubebuilder:validation:Enum=Create;Adopt
CreationPolicy CreationPolicy `json:"creationPolicy,omitempty"`
}

// DeployKey defines an SSH key to be used for git operations.
Expand Down Expand Up @@ -147,6 +157,11 @@ func (g *GitRepo) GetDeletionPolicy() DeletionPolicy {
return g.Spec.DeletionPolicy
}

// GetCreationPolicy returns the object's creation policy
func (g *GitRepo) GetCreationPolicy() CreationPolicy {
return g.Spec.CreationPolicy
}

// GetDisplayName returns the display name of the object
func (g *GitRepo) GetDisplayName() string {
return g.Spec.DisplayName
Expand Down
10 changes: 10 additions & 0 deletions api/v1alpha1/tenant_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ type TenantSpec struct {
// Archive: will archive the external resources, if it supports that
// +kubebuilder:validation:Enum=Delete;Retain;Archive
DeletionPolicy DeletionPolicy `json:"deletionPolicy,omitempty"`
// CreationPolicy defines how the external resources should be treated upon CR creation.
// Create: will only create a new external resource and will not manage already existing resources
// Adopt: will create a new external resource or will adopt and manage an already existing resource
// +kubebuilder:validation:Enum=Create;Adopt
CreationPolicy CreationPolicy `json:"creationPolicy,omitempty"`
// ClusterTemplate defines a template which will be used to set defaults for the clusters of this tenant.
// The fields within this can use Go templating.
// See https://syn.tools/lieutenant-operator/explanations/templating.html for details.
Expand Down Expand Up @@ -85,6 +90,11 @@ func (t *Tenant) GetDeletionPolicy() DeletionPolicy {
return t.Spec.DeletionPolicy
}

// GetCreationPolicy returns the object's creation policy
func (t *Tenant) GetCreationPolicy() CreationPolicy {
return t.Spec.CreationPolicy
}

// GetDisplayName returns the display name of the object
func (t *Tenant) GetDisplayName() string {
return t.Spec.DisplayName
Expand Down
20 changes: 20 additions & 0 deletions config/crd/bases/syn.tools_clusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ spec:
spec:
description: ClusterSpec defines the desired state of Cluster
properties:
creationPolicy:
description: 'CreationPolicy defines how the external resources should
be treated upon CR creation. Create: will only create a new external
resource and will not manage already existing resources Adopt: will
create a new external resource or will adopt and manage an already
existing resource'
enum:
- Create
- Adopt
type: string
deletionPolicy:
description: 'DeletionPolicy defines how the external resources should
be treated upon CR deletion. Retain: will not delete any external
Expand Down Expand Up @@ -84,6 +94,16 @@ spec:
secret name must be unique.
type: string
type: object
creationPolicy:
description: 'CreationPolicy defines how the external resources
should be treated upon CR creation. Create: will only create
a new external resource and will not manage already existing
resources Adopt: will create a new external resource or will
adopt and manage an already existing resource'
enum:
- Create
- Adopt
type: string
deletionPolicy:
description: 'DeletionPolicy defines how the external resources
should be treated upon CR deletion. Retain: will not delete
Expand Down
10 changes: 10 additions & 0 deletions config/crd/bases/syn.tools_gitrepos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ spec:
name must be unique.
type: string
type: object
creationPolicy:
description: 'CreationPolicy defines how the external resources should
be treated upon CR creation. Create: will only create a new external
resource and will not manage already existing resources Adopt: will
create a new external resource or will adopt and manage an already
existing resource'
enum:
- Create
- Adopt
type: string
deletionPolicy:
description: 'DeletionPolicy defines how the external resources should
be treated upon CR deletion. Retain: will not delete any external
Expand Down
40 changes: 40 additions & 0 deletions config/crd/bases/syn.tools_tenants.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ spec:
this can use Go templating. See https://syn.tools/lieutenant-operator/explanations/templating.html
for details.
properties:
creationPolicy:
description: 'CreationPolicy defines how the external resources
should be treated upon CR creation. Create: will only create
a new external resource and will not manage already existing
resources Adopt: will create a new external resource or will
adopt and manage an already existing resource'
enum:
- Create
- Adopt
type: string
deletionPolicy:
description: 'DeletionPolicy defines how the external resources
should be treated upon CR deletion. Retain: will not delete
Expand Down Expand Up @@ -90,6 +100,16 @@ spec:
the secret name must be unique.
type: string
type: object
creationPolicy:
description: 'CreationPolicy defines how the external resources
should be treated upon CR creation. Create: will only create
a new external resource and will not manage already existing
resources Adopt: will create a new external resource or
will adopt and manage an already existing resource'
enum:
- Create
- Adopt
type: string
deletionPolicy:
description: 'DeletionPolicy defines how the external resources
should be treated upon CR deletion. Retain: will not delete
Expand Down Expand Up @@ -174,6 +194,16 @@ spec:
description: TokenLifetime set the token lifetime
type: string
type: object
creationPolicy:
description: 'CreationPolicy defines how the external resources should
be treated upon CR creation. Create: will only create a new external
resource and will not manage already existing resources Adopt: will
create a new external resource or will adopt and manage an already
existing resource'
enum:
- Create
- Adopt
type: string
deletionPolicy:
description: 'DeletionPolicy defines how the external resources should
be treated upon CR deletion. Retain: will not delete any external
Expand Down Expand Up @@ -209,6 +239,16 @@ spec:
secret name must be unique.
type: string
type: object
creationPolicy:
description: 'CreationPolicy defines how the external resources
should be treated upon CR creation. Create: will only create
a new external resource and will not manage already existing
resources Adopt: will create a new external resource or will
adopt and manage an already existing resource'
enum:
- Create
- Adopt
type: string
deletionPolicy:
description: 'DeletionPolicy defines how the external resources
should be treated upon CR deletion. Retain: will not delete
Expand Down
40 changes: 40 additions & 0 deletions config/crd/bases/syn.tools_tenanttemplates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ spec:
this can use Go templating. See https://syn.tools/lieutenant-operator/explanations/templating.html
for details.
properties:
creationPolicy:
description: 'CreationPolicy defines how the external resources
should be treated upon CR creation. Create: will only create
a new external resource and will not manage already existing
resources Adopt: will create a new external resource or will
adopt and manage an already existing resource'
enum:
- Create
- Adopt
type: string
deletionPolicy:
description: 'DeletionPolicy defines how the external resources
should be treated upon CR deletion. Retain: will not delete
Expand Down Expand Up @@ -90,6 +100,16 @@ spec:
the secret name must be unique.
type: string
type: object
creationPolicy:
description: 'CreationPolicy defines how the external resources
should be treated upon CR creation. Create: will only create
a new external resource and will not manage already existing
resources Adopt: will create a new external resource or
will adopt and manage an already existing resource'
enum:
- Create
- Adopt
type: string
deletionPolicy:
description: 'DeletionPolicy defines how the external resources
should be treated upon CR deletion. Retain: will not delete
Expand Down Expand Up @@ -174,6 +194,16 @@ spec:
description: TokenLifetime set the token lifetime
type: string
type: object
creationPolicy:
description: 'CreationPolicy defines how the external resources should
be treated upon CR creation. Create: will only create a new external
resource and will not manage already existing resources Adopt: will
create a new external resource or will adopt and manage an already
existing resource'
enum:
- Create
- Adopt
type: string
deletionPolicy:
description: 'DeletionPolicy defines how the external resources should
be treated upon CR deletion. Retain: will not delete any external
Expand Down Expand Up @@ -209,6 +239,16 @@ spec:
secret name must be unique.
type: string
type: object
creationPolicy:
description: 'CreationPolicy defines how the external resources
should be treated upon CR creation. Create: will only create
a new external resource and will not manage already existing
resources Adopt: will create a new external resource or will
adopt and manage an already existing resource'
enum:
- Create
- Adopt
type: string
deletionPolicy:
description: 'DeletionPolicy defines how the external resources
should be treated upon CR deletion. Retain: will not delete
Expand Down
16 changes: 9 additions & 7 deletions controllers/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type ClusterReconciler struct {
client.Client
Scheme *runtime.Scheme

CreateSATokenSecret bool
CreateSATokenSecret bool
DefaultCreationPolicy synv1alpha1.CreationPolicy
}

//+kubebuilder:rbac:groups=syn.tools,resources=clusters,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -47,12 +48,13 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, request ctrl.Request)
}

data := &pipeline.Context{
Context: ctx,
Client: r.Client,
Log: reqLogger,
FinalizerName: synv1alpha1.FinalizerName,
Reconciler: r,
CreateSATokenSecret: r.CreateSATokenSecret,
Context: ctx,
Client: r.Client,
Log: reqLogger,
FinalizerName: synv1alpha1.FinalizerName,
Reconciler: r,
CreateSATokenSecret: r.CreateSATokenSecret,
DefaultCreationPolicy: r.DefaultCreationPolicy,
}

steps := []pipeline.Step{
Expand Down
2 changes: 1 addition & 1 deletion controllers/gitrepo/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func steps(obj pipeline.Object, data *pipeline.Context, getGitClient gitClientFa
data.Log.Info("successfully created the repository")
}

if instance.Status.URL != repo.FullURL().String() {
if instance.Status.URL != repo.FullURL().String() && instance.Spec.CreationPolicy != synv1alpha1.AdoptPolicy {
var err error
if !data.Deleted {
phase := synv1alpha1.Failed
Expand Down
35 changes: 32 additions & 3 deletions controllers/gitrepo/steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestSteps(t *testing.T) {
name string
repoType synv1alpha1.RepoType
deleted bool
adopt bool
statusURL string

shouldError bool
Expand Down Expand Up @@ -121,6 +122,18 @@ func TestSteps(t *testing.T) {
name: "bar",
repoType: synv1alpha1.AutoRepoType,
},
"should adopt repo": {
exists: true,
repoUrl: "git.example.com/foo/bar",
adopt: true,

path: "foo",
name: "bar",
repoType: synv1alpha1.AutoRepoType,

shouldUpdate: true,
updatedStatusURL: "git.example.com/foo/bar",
},
"should not delete unadopted repo": {
exists: true,
repoUrl: "git.example.com/foo/bar",
Expand All @@ -130,6 +143,18 @@ func TestSteps(t *testing.T) {
repoType: synv1alpha1.AutoRepoType,
deleted: true,
},
"should adopt and delete repo": {
exists: true,
repoUrl: "git.example.com/foo/bar",
adopt: true,

path: "foo",
name: "bar",
repoType: synv1alpha1.AutoRepoType,
deleted: true,

shouldDelete: true,
},
"should create other repo": {
exists: false,
repoUrl: "git.example.com/foo/bar",
Expand All @@ -154,15 +179,19 @@ func TestSteps(t *testing.T) {
},
Spec: synv1alpha1.GitRepoSpec{
GitRepoTemplate: synv1alpha1.GitRepoTemplate{
Path: tc.path,
RepoName: tc.name,
RepoType: tc.repoType,
Path: tc.path,
RepoName: tc.name,
RepoType: tc.repoType,
CreationPolicy: synv1alpha1.CreatePolicy,
},
},
Status: synv1alpha1.GitRepoStatus{
URL: tc.statusURL,
},
}
if tc.adopt {
repo.Spec.GitRepoTemplate.CreationPolicy = synv1alpha1.AdoptPolicy
}
c := fake.NewClientBuilder().
WithScheme(scheme).
WithObjects(repo).
Expand Down
7 changes: 7 additions & 0 deletions controllers/gitrepo/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ func CreateOrUpdate(obj pipeline.Object, data *pipeline.Context) pipeline.Result
template.DeletionPolicy = obj.GetDeletionPolicy()
}
}
if template.CreationPolicy == "" {
if obj.GetCreationPolicy() == "" {
template.CreationPolicy = data.DefaultCreationPolicy
} else {
template.CreationPolicy = obj.GetCreationPolicy()
}
}

if template.RepoType == synv1alpha1.DefaultRepoType {
template.RepoType = synv1alpha1.AutoRepoType
Expand Down
Loading

0 comments on commit 6654916

Please sign in to comment.