Skip to content

Commit

Permalink
Merge pull request #13 from projectsyn/fix_path
Browse files Browse the repository at this point in the history
More integration fixes
  • Loading branch information
Simon Rüegg authored Feb 14, 2020
2 parents cc1f688 + 7d920bf commit 55e5427
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 66 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Reuse object names for GitRepo names
### Fixed
- Gitlab subgroup handling
- GitRepos properly updated from Clusters and Tenants

## v0.0.3 - 2019-02-10
### Added
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4 h1:hU4mGcQI4DaAYW+IbTun+2qEZVFxK0ySjQLTbS0VQKc=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
github.com/googleapis/gnostic v0.2.0/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
Expand Down Expand Up @@ -743,6 +744,7 @@ gitlab.com/nyarla/go-crypt v0.0.0-20160106005555-d9a5dc2b789b/go.mod h1:T3BPAOm2
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.mongodb.org/mongo-driver v1.1.0/go.mod h1:u7ryQJ+DOzQmeO7zB6MHyr8jkEQvC8vH7qLUO4lqsUM=
go.opencensus.io v0.20.1/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk=
go.opencensus.io v0.21.0 h1:mU6zScU4U1YAFPHEHYk+3JC4SY7JxgkqS10ZOSyksNg=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.uber.org/atomic v0.0.0-20181018215023-8dc6146f7569/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
Expand Down Expand Up @@ -900,6 +902,7 @@ gonum.org/v1/netlib v0.0.0-20190331212654-76723241ea4e/go.mod h1:kS+toOQn6AQKjmK
google.golang.org/api v0.3.1/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
google.golang.org/api v0.3.2/go.mod h1:6wY9I6uQWHQ8EM57III9mq/AjF+i8G65rmVagqKMtkk=
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
google.golang.org/api v0.6.1-0.20190607001116-5213b8090861 h1:ppLucX0K/60T3t6LPZQzTOkt5PytkEbQLIaSteq+TpE=
google.golang.org/api v0.6.1-0.20190607001116-5213b8090861/go.mod h1:btoxGiFvQNVUZQ8W08zLtrVS08CNpINPEfxXxgJL1Q4=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
Expand Down
41 changes: 11 additions & 30 deletions pkg/controller/cluster/cluster_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

Expand Down Expand Up @@ -48,38 +47,18 @@ func (r *ReconcileCluster) Reconcile(request reconcile.Request) (reconcile.Resul
if time.Now().After(instance.Status.BootstrapToken.ValidUntil.Time) {
instance.Status.BootstrapToken.TokenValid = false
}
if instance.Spec.GitRepoURL == "" {
gvk := schema.GroupVersionKind{
Version: instance.APIVersion,
Kind: instance.Kind,
}

created, err := helpers.CreateGitRepo(instance, gvk, instance.Spec.GitRepoTemplate, r.client, instance.Spec.TenantRef)
if err != nil {
reqLogger.Error(err, "Cannot create git repo object")
return reconcile.Result{}, err
}
gvk := schema.GroupVersionKind{
Version: instance.APIVersion,
Kind: instance.Kind,
}

if !created {
gitRepo := &synv1alpha1.GitRepo{}
repoNamespacedName := types.NamespacedName{
Namespace: instance.GetNamespace(),
Name: instance.Name,
}
err = r.client.Get(context.TODO(), repoNamespacedName, gitRepo)
if err != nil {
return reconcile.Result{}, err
}

if gitRepo.Status.Phase != nil && *gitRepo.Status.Phase == synv1alpha1.Created {
instance.Spec.GitRepoURL = gitRepo.Status.URL
if len(gitRepo.Status.HostKeys) > 0 &&
len(instance.Spec.GitHostKeys) == 0 {
instance.Spec.GitHostKeys = gitRepo.Status.HostKeys
}
}
}
err = helpers.CreateOrUpdateGitRepo(instance, gvk, instance.Spec.GitRepoTemplate, r.client, instance.Spec.TenantRef)
if err != nil {
reqLogger.Error(err, "Cannot create or update git repo object")
return reconcile.Result{}, err
}

helpers.AddTenantLabel(&instance.ObjectMeta, instance.Spec.TenantRef.Name)
err = r.client.Status().Update(context.TODO(), instance)
if err != nil {
Expand Down Expand Up @@ -124,3 +103,5 @@ func (r *ReconcileCluster) newStatus(cluster *synv1alpha1.Cluster) error {
}
return nil
}

//TODO: update git if template changes
2 changes: 2 additions & 0 deletions pkg/controller/gitrepo/gitrepo_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ func (r *ReconcileGitRepo) Reconcile(request reconcile.Request) (reconcile.Resul
reqLogger.Info("keys differed from CRD, keys re-applied to repository")
}

helpers.AddTenantLabel(&instance.ObjectMeta, instance.Spec.TenantRef.Name)

return reconcile.Result{}, r.client.Status().Update(context.TODO(), instance)
}

Expand Down
31 changes: 7 additions & 24 deletions pkg/controller/tenant/tenant_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/projectsyn/lieutenant-operator/pkg/helpers"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

Expand All @@ -33,30 +32,14 @@ func (r *ReconcileTenant) Reconcile(request reconcile.Request) (reconcile.Result
return reconcile.Result{}, err
}

if instance.Spec.GitRepoURL == "" {
gvk := schema.GroupVersionKind{
Version: instance.APIVersion,
Kind: instance.Kind,
}
gvk := schema.GroupVersionKind{
Version: instance.APIVersion,
Kind: instance.Kind,
}

created, err := helpers.CreateGitRepo(instance, gvk, instance.Spec.GitRepoTemplate, r.client, corev1.LocalObjectReference{Name: instance.GetName()})
if err != nil {
return reconcile.Result{}, err
}
if !created {
gitRepo := &synv1alpha1.GitRepo{}
repoNamespacedName := types.NamespacedName{
Namespace: instance.GetNamespace(),
Name: instance.GetName(),
}
err = r.client.Get(context.TODO(), repoNamespacedName, gitRepo)
if err != nil {
return reconcile.Result{}, err
}
if gitRepo.Status.Phase != nil && *gitRepo.Status.Phase == synv1alpha1.Created {
instance.Spec.GitRepoURL = gitRepo.Status.URL
}
}
err = helpers.CreateOrUpdateGitRepo(instance, gvk, instance.Spec.GitRepoTemplate, r.client, corev1.LocalObjectReference{Name: instance.GetName()})
if err != nil {
return reconcile.Result{}, err
}
return reconcile.Result{}, r.client.Update(context.TODO(), instance)
}
9 changes: 8 additions & 1 deletion pkg/git/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,14 @@ func (g *Gitlab) Connect() error {

// FullURL returns the complete url of this git repository
func (g *Gitlab) FullURL() *url.URL {
return g.ops.URL

sshURL := g.ops.URL

sshURL.Scheme = "ssh"
sshURL.User = url.User("git")
sshURL.Path = sshURL.Path + ".git"

return sshURL
}

// IsType determines if the given url can be handled by this concrete implementation.
Expand Down
2 changes: 1 addition & 1 deletion pkg/git/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Credentials struct {
type Repo interface {
// Type returns the type of the repo
Type() string
// FullURL returns the full url to the repository
// FullURL returns the full url to the repository for ssh pulling
FullURL() *url.URL
Create() error
// Update will enforce the defined keys to be deployed to the repository, it will return true if an actual change
Expand Down
21 changes: 11 additions & 10 deletions pkg/helpers/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package helpers
import (
"context"
"fmt"
"reflect"

corev1 "k8s.io/api/core/v1"

Expand All @@ -15,15 +16,15 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

// CreateGitRepo will create the gitRepo object if it doesn't already exist. If the owner object itself is a tenant tenantRef can be set nil.
func CreateGitRepo(obj metav1.Object, gvk schema.GroupVersionKind, template *synv1alpha1.GitRepoTemplate, client client.Client, tenantRef corev1.LocalObjectReference) (bool, error) {
// CreateOrUpdateGitRepo will create the gitRepo object if it doesn't already exist. If the owner object itself is a tenant tenantRef can be set nil.
func CreateOrUpdateGitRepo(obj metav1.Object, gvk schema.GroupVersionKind, template *synv1alpha1.GitRepoTemplate, client client.Client, tenantRef corev1.LocalObjectReference) error {

if template == nil {
return false, fmt.Errorf("gitRepo template is empty")
return fmt.Errorf("gitRepo template is empty")
}

if tenantRef.Name == "" {
return false, fmt.Errorf("the tenant name is empty")
return fmt.Errorf("the tenant name is empty")
}

repo := &synv1alpha1.GitRepo{
Expand Down Expand Up @@ -51,16 +52,16 @@ func CreateGitRepo(obj metav1.Object, gvk schema.GroupVersionKind, template *syn

err = client.Get(context.TODO(), namespacedName, existingRepo)
if err != nil {
return false, fmt.Errorf("could not update existing repo: %v", err)
return fmt.Errorf("could not update existing repo: %v", err)
}

existingRepo.Spec = repo.Spec
if !reflect.DeepEqual(existingRepo.Spec, repo.Spec) {
existingRepo.Spec = repo.Spec

return false, client.Update(context.TODO(), existingRepo)
} else if err != nil {
return false, err
err = client.Update(context.TODO(), existingRepo)
}
}
return true, nil
return err
}

// AddTenantLabel adds the tenant label to an object
Expand Down
124 changes: 124 additions & 0 deletions pkg/helpers/crd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package helpers

import (
"context"
"testing"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

synv1alpha1 "github.com/projectsyn/lieutenant-operator/pkg/apis/syn/v1alpha1"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/projectsyn/lieutenant-operator/pkg/apis"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

func TestAddTenantLabel(t *testing.T) {
type args struct {
meta *metav1.ObjectMeta
tenant string
}
tests := []struct {
name string
args args
}{
{
name: "add labels",
args: args{
meta: &metav1.ObjectMeta{},
tenant: "test",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
AddTenantLabel(tt.args.meta, tt.args.tenant)

if tt.args.meta.Labels[apis.LabelNameTenant] != tt.args.tenant {
t.Error("labels do not match")
}

})
}
}

func TestCreateOrUpdateGitRepo(t *testing.T) {
type args struct {
obj metav1.Object
gvk schema.GroupVersionKind
template *synv1alpha1.GitRepoTemplate
tenantRef v1.LocalObjectReference
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "create and update git repo",
args: args{
obj: &synv1alpha1.GitRepo{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
},
gvk: schema.GroupVersionKind{
Version: "testVersion",
Kind: "testKind",
},
template: &synv1alpha1.GitRepoTemplate{
APISecretRef: v1.SecretReference{Name: "testSecret"},
DeployKeys: nil,
Path: "testPath",
RepoName: "testRepo",
},
tenantRef: v1.LocalObjectReference{
Name: "testTenant",
},
},
},
}
for _, tt := range tests {

objs := []runtime.Object{
&synv1alpha1.GitRepo{},
}

cl := testSetupClient(objs)

t.Run(tt.name, func(t *testing.T) {
if err := CreateOrUpdateGitRepo(tt.args.obj, tt.args.gvk, tt.args.template, cl, tt.args.tenantRef); (err != nil) != tt.wantErr {
t.Errorf("CreateOrUpdateGitRepo() error = %v, wantErr %v", err, tt.wantErr)
}
})

namespacedName := types.NamespacedName{
Name: tt.args.obj.GetName(),
Namespace: tt.args.obj.GetNamespace(),
}

checkRepo := &synv1alpha1.GitRepo{}
assert.NoError(t, cl.Get(context.TODO(), namespacedName, checkRepo))
assert.Equal(t, tt.args.template, &checkRepo.Spec.GitRepoTemplate)
tt.args.template.RepoName = "changedName"
assert.NoError(t, CreateOrUpdateGitRepo(tt.args.obj, tt.args.gvk, tt.args.template, cl, tt.args.tenantRef))
assert.NoError(t, cl.Get(context.TODO(), namespacedName, checkRepo))
assert.Equal(t, tt.args.template, &checkRepo.Spec.GitRepoTemplate)

}
}

// testSetupClient returns a client containing all objects in objs
func testSetupClient(objs []runtime.Object) client.Client {
s := scheme.Scheme
s.AddKnownTypes(synv1alpha1.SchemeGroupVersion, objs...)
return fake.NewFakeClient(objs...)
}

0 comments on commit 55e5427

Please sign in to comment.