Skip to content

Commit

Permalink
fix: correct the component spec when get jobservice cr
Browse files Browse the repository at this point in the history
Signed-off-by: He Weiwei <hweiwei@vmware.com>
  • Loading branch information
heww committed Jan 9, 2021
1 parent 6209cc9 commit e04da2c
Show file tree
Hide file tree
Showing 17 changed files with 432 additions and 3 deletions.
15 changes: 15 additions & 0 deletions controllers/goharbor/harbor/harbor_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package harbor_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestHarbor(t *testing.T) {
t.Parallel()

RegisterFailHandler(Fail)
RunSpecs(t, "Harbor Suite")
}
114 changes: 114 additions & 0 deletions controllers/goharbor/harbor/harbor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package harbor_test

import (
"context"
"io/ioutil"
"strings"

goharborv1alpha2 "github.com/goharbor/harbor-operator/apis/goharbor.io/v1alpha2"
"github.com/goharbor/harbor-operator/controllers"
"github.com/goharbor/harbor-operator/controllers/goharbor/harbor"
"github.com/goharbor/harbor-operator/pkg/config"
"github.com/goharbor/harbor-operator/pkg/factories/application"
"github.com/goharbor/harbor-operator/pkg/factories/logger"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/yaml"
)

func fileString(filePath string) string {
content, err := ioutil.ReadFile(filePath)
Expect(err).NotTo(HaveOccurred())

return strings.TrimSpace(string(content))
}

var _ = Describe("Harbor", func() {
var (
ctx context.Context
decoder runtime.Decoder
r *harbor.Reconciler

input string
getComponent func(*goharborv1alpha2.Harbor) runtime.Object
output string
)

BeforeEach(func() {
ctx = logger.Context(zap.LoggerTo(GinkgoWriter, true))
application.SetName(&ctx, "operator")
application.SetVersion(&ctx, "dev")

name := controllers.Harbor.String()

configStore := config.NewConfigWithDefaults()
configStore.Env(name)

i, err := harbor.New(ctx, name, configStore)
Expect(err).NotTo(HaveOccurred())
r = i.(*harbor.Reconciler)

sch := runtime.NewScheme()
_ = goharborv1alpha2.AddToScheme(sch)

decoder = serializer.NewCodecFactory(sch).UniversalDeserializer()

input = ""
getComponent = nil
output = ""
})

JustBeforeEach(func() {
obj, _, err := decoder.Decode([]byte(input), nil, nil)
Expect(err).NotTo(HaveOccurred())

h, ok := obj.(*goharborv1alpha2.Harbor)
Expect(ok).To(BeTrue())

bytes, err := yaml.Marshal(getComponent(h))
Expect(err).NotTo(HaveOccurred())

output = strings.TrimSpace(string(bytes))
})

Context("GetJobService", func() {
BeforeEach(func() {
getComponent = func(h *goharborv1alpha2.Harbor) runtime.Object {
j, err := r.GetJobService(ctx, h)
Expect(err).NotTo(HaveOccurred())

j.SetGroupVersionKind(schema.FromAPIVersionAndKind("goharbor.io/v1alpha2", "JobService"))

return j
}
})

for _, text := range []string{
"Default",
"Repository and tag suffix not empty",
"Repository not empty",
"Tag suffix not empty",
"Version and repository not empty",
"Version not empty",
} {
text := text

filename := strings.Join(strings.Split(strings.ToLower(text), " "), "-")

Context(text, func() {
BeforeEach(func() {
input = fileString("./manifests/jobservice/" + filename + ".yaml")
})

It("Should pass", func() {
expected := fileString("./manifests/jobservice/" + filename + "-expected.yaml")
Expect(output).To(BeEquivalentTo(expected))
})
})
}
})
})
2 changes: 1 addition & 1 deletion controllers/goharbor/harbor/jobservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (r *Reconciler) GetJobService(ctx context.Context, harbor *goharborv1alpha2
Annotations: version.SetVersion(nil, harbor.Spec.Version),
},
Spec: goharborv1alpha2.JobServiceSpec{
ComponentSpec: harbor.Spec.Registry.ComponentSpec,
ComponentSpec: r.getComponentSpec(ctx, harbor, harbormetav1.JobServiceComponent),
Core: goharborv1alpha2.JobServiceCoreSpec{
SecretRef: coreSecretRef,
URL: coreURL,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
apiVersion: goharbor.io/v1alpha2
kind: JobService
metadata:
annotations:
harbor.goharbor.io/version: ""
creationTimestamp: null
name: example-harbor
namespace: default
spec:
core:
secretRef: example-harbor-core-secret
url: http://example-harbor-core:80
jobLoggers:
files:
- level: INFO
sweeper: 14h0m0s
loggers:
stdout:
level: INFO
registry:
controllerURL: http://example-harbor-registryctl:80
credentials:
passwordRef: example-harbor-registry-basicauth
username: harbor_registry_user
url: http://example-harbor-registry
resources: {}
secretRef: example-harbor-jobservice-secret
tokenService:
url: http://example-harbor-core/service/token
workerPool:
redisPool:
database: 2
host: 127.0.0.1
port: 3306
status:
conditions: []
message: ""
status: ""

9 changes: 9 additions & 0 deletions controllers/goharbor/harbor/manifests/jobservice/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: goharbor.io/v1alpha2
kind: Harbor
metadata:
name: example
namespace: default
spec:
redis:
host: 127.0.0.1
port: 3306
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
apiVersion: goharbor.io/v1alpha2
kind: JobService
metadata:
annotations:
harbor.goharbor.io/version: ""
creationTimestamp: null
name: example-harbor
namespace: default
spec:
core:
secretRef: example-harbor-core-secret
url: http://example-harbor-core:80
image: example.goharbor.io/goharbor/harbor-jobservice:v2.1.2-patch1
jobLoggers:
files:
- level: INFO
sweeper: 14h0m0s
loggers:
stdout:
level: INFO
registry:
controllerURL: http://example-harbor-registryctl:80
credentials:
passwordRef: example-harbor-registry-basicauth
username: harbor_registry_user
url: http://example-harbor-registry
resources: {}
secretRef: example-harbor-jobservice-secret
tokenService:
url: http://example-harbor-core/service/token
workerPool:
redisPool:
database: 2
host: 127.0.0.1
port: 3306
status:
conditions: []
message: ""
status: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: goharbor.io/v1alpha2
kind: Harbor
metadata:
name: example
namespace: default
spec:
imageSource:
repository: example.goharbor.io/goharbor
tagSuffix: -patch1
redis:
host: 127.0.0.1
port: 3306
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
apiVersion: goharbor.io/v1alpha2
kind: JobService
metadata:
annotations:
harbor.goharbor.io/version: ""
creationTimestamp: null
name: example-harbor
namespace: default
spec:
core:
secretRef: example-harbor-core-secret
url: http://example-harbor-core:80
image: example.goharbor.io/goharbor/harbor-jobservice:v2.1.2
jobLoggers:
files:
- level: INFO
sweeper: 14h0m0s
loggers:
stdout:
level: INFO
registry:
controllerURL: http://example-harbor-registryctl:80
credentials:
passwordRef: example-harbor-registry-basicauth
username: harbor_registry_user
url: http://example-harbor-registry
resources: {}
secretRef: example-harbor-jobservice-secret
tokenService:
url: http://example-harbor-core/service/token
workerPool:
redisPool:
database: 2
host: 127.0.0.1
port: 3306
status:
conditions: []
message: ""
status: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: goharbor.io/v1alpha2
kind: Harbor
metadata:
name: example
namespace: default
spec:
imageSource:
repository: example.goharbor.io/goharbor
redis:
host: 127.0.0.1
port: 3306
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
apiVersion: goharbor.io/v1alpha2
kind: JobService
metadata:
annotations:
harbor.goharbor.io/version: ""
creationTimestamp: null
name: example-harbor
namespace: default
spec:
core:
secretRef: example-harbor-core-secret
url: http://example-harbor-core:80
image: goharbor/harbor-jobservice:v2.1.2-patch1
jobLoggers:
files:
- level: INFO
sweeper: 14h0m0s
loggers:
stdout:
level: INFO
registry:
controllerURL: http://example-harbor-registryctl:80
credentials:
passwordRef: example-harbor-registry-basicauth
username: harbor_registry_user
url: http://example-harbor-registry
resources: {}
secretRef: example-harbor-jobservice-secret
tokenService:
url: http://example-harbor-core/service/token
workerPool:
redisPool:
database: 2
host: 127.0.0.1
port: 3306
status:
conditions: []
message: ""
status: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: goharbor.io/v1alpha2
kind: Harbor
metadata:
name: example
namespace: default
spec:
imageSource:
tagSuffix: -patch1
redis:
host: 127.0.0.1
port: 3306
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
apiVersion: goharbor.io/v1alpha2
kind: JobService
metadata:
annotations:
harbor.goharbor.io/version: 2.1.0
creationTimestamp: null
name: example-harbor
namespace: default
spec:
core:
secretRef: example-harbor-core-secret
url: http://example-harbor-core:80
image: example.goharbor.io/goharbor/harbor-jobservice:v2.1.0
jobLoggers:
files:
- level: INFO
sweeper: 14h0m0s
loggers:
stdout:
level: INFO
registry:
controllerURL: http://example-harbor-registryctl:80
credentials:
passwordRef: example-harbor-registry-basicauth
username: harbor_registry_user
url: http://example-harbor-registry
resources: {}
secretRef: example-harbor-jobservice-secret
tokenService:
url: http://example-harbor-core/service/token
workerPool:
redisPool:
database: 2
host: 127.0.0.1
port: 3306
status:
conditions: []
message: ""
status: ""

0 comments on commit e04da2c

Please sign in to comment.