Skip to content

Commit

Permalink
fix(statuscheck) Ensure conditions are ok
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Péronnet <pierre.peronnet@ovhcloud.com>
  • Loading branch information
holyhope committed Jan 18, 2021
1 parent 5a81872 commit 3157529
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 134 deletions.
24 changes: 19 additions & 5 deletions controllers/goharbor/chartmuseum/chartmuseum.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,18 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) err
return errors.Wrap(err, "cannot setup common controller")
}

className, err := r.ConfigStore.GetItemValue(config.HarborClassKey)
className := ""

configItem, err := configstore.Filter().Store(r.ConfigStore).Slice(config.HarborClassKey).GetFirstItem()
if err != nil {
return errors.Wrap(err, "cannot get harbor class")
if _, ok := err.(configstore.ErrItemNotFound); !ok {
return errors.Wrap(err, "cannot get config template path")
}
} else {
className, err = configItem.Value()
if err != nil {
return errors.Wrap(err, "invalid config template path")
}
}

concurrentReconcile, err := r.ConfigStore.GetItemValueInt(config.ReconciliationKey)
Expand Down Expand Up @@ -79,13 +88,18 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) err
}

func New(ctx context.Context, name string, configStore *configstore.Store) (commonCtrl.Reconciler, error) {
configTemplatePath, err := configStore.GetItemValue(ConfigTemplatePathKey)
configTemplatePath := DefaultConfigTemplatePath

configItem, err := configstore.Filter().Store(configStore).Slice(ConfigTemplatePathKey).GetFirstItem()
if err != nil {
if !config.IsNotFound(err, ConfigTemplatePathKey) {
return nil, errors.Wrap(err, "cannot get config template path")
}

configTemplatePath = DefaultConfigTemplatePath
} else {
configTemplatePath, err = configItem.Value()
if err != nil {
return nil, errors.Wrap(err, "invalid config template path")
}
}

r := &Reconciler{
Expand Down
88 changes: 28 additions & 60 deletions controllers/goharbor/chartmuseum/chartmuseum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ limitations under the License.
package chartmuseum_test

import (
"bytes"
"context"
"encoding/json"
"fmt"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"golang.org/x/net/html"

goharborv1alpha2 "github.com/goharbor/harbor-operator/apis/goharbor.io/v1alpha2"
harbormetav1 "github.com/goharbor/harbor-operator/apis/meta/v1alpha1"
Expand All @@ -47,10 +46,13 @@ var _ = Describe("ChartMuseum", func() {
chartMuseum.ObjectMeta = metav1.ObjectMeta{
Name: test.NewName("chartmuseum"),
Namespace: ns.GetName(),
Annotations: map[string]string{
goharborv1alpha2.HarborClassAnnotation: harborClass,
},
}
})

JustAfterEach(test.LogOnFailureFunc(&ctx, func() types.NamespacedName {
JustAfterEach(test.LogsAll(&ctx, func() types.NamespacedName {
return types.NamespacedName{
Name: reconciler.NormalizeName(ctx, chartMuseum.GetName()),
Namespace: chartMuseum.GetNamespace(),
Expand Down Expand Up @@ -86,24 +88,19 @@ var _ = Describe("ChartMuseum", func() {
Ω(chartMuseum.GetGeneration()).
Should(Equal(defaultGenerationNumber), "Generation should not be updated")

Eventually(func() (int64, error) {
err := test.GetClient(ctx).Create(ctx, &chartMuseum)
if err != nil {
return 0, err
}

return chartMuseum.Status.ObservedGeneration, nil
}, 5*time.Second).
Should(Equal(chartMuseum.GetGeneration()), "ObservedGeneration should be updated")

test.EnsureReady(ctx, &chartMuseum, time.Minute, 5*time.Second)

IntegTest(ctx, &chartMuseum)
})

By("Updating resource spec", func() {
oldGeneration := chartMuseum.GetGeneration()

test.ScaleUp(ctx, &chartMuseum)

Ω(chartMuseum.GetGeneration()).
Should(BeNumerically(">", oldGeneration), "ObservedGeneration should be updated")

Ω(test.GetClient(ctx).Get(ctx, test.GetNamespacedName(&chartMuseum), &chartMuseum)).
Should(Succeed(), "resource should still be accessible")

Expand All @@ -113,7 +110,8 @@ var _ = Describe("ChartMuseum", func() {
})

By("Deleting resource", func() {
Ω(test.GetClient(ctx).Delete(ctx, &chartMuseum)).Should(Succeed())
Ω(test.GetClient(ctx).Delete(ctx, &chartMuseum)).
Should(Succeed())

Eventually(func() error {
return test.GetClient(ctx).Get(ctx, test.GetNamespacedName(&chartMuseum), &chartMuseum)
Expand All @@ -133,55 +131,25 @@ func IntegTest(ctx context.Context, chartMuseum *goharborv1alpha2.ChartMuseum) {
Namespace: chartMuseum.GetNamespace(),
}

/*
Eventually(func() error {
endpoint := &corev1.Endpoints{}
// Make sure chart museum is up and running
time.Sleep(60 * time.Second)

err := test.GetClient(ctx).Get(ctx, namespacedName, endpoint)
if err != nil {
return err
}
proxyReq := client.Get().
Resource("services").
Namespace(namespacedName.Namespace).
Name(fmt.Sprintf("%s:%s", namespacedName.Name, harbormetav1.ChartMuseumHTTPPortName)).
SubResource("proxy").
Suffix("health")

ok, err := statuscheck.EndpointCheck(ctx, endpoint, harbormetav1.ChartMuseumHTTPPortName)
if err != nil {
return err
Ω(proxyReq.DoRaw(ctx)).
Should(WithTransform(func(result []byte) bool {
var health struct {
Healthy bool `json:"healthy"`
}

if !ok {
return errors.New("not ready") // nolint:goerr113
}
Ω(json.Unmarshal(result, &health)).
Should(Succeed())

return nil
}, 30*time.Second, 100*time.Millisecond).Should(Succeed())
var pods corev1.PodList
Ω(
client.Get().
Resource("pods").
Namespace(chartMuseum.GetNamespace()).
Param("labelSelector", strings.Join([]string{
fmt.Sprintf("%s=%s", reconciler.Label("name"), reconciler.NormalizeName(ctx, chartMuseum.Name)),
fmt.Sprintf("%s=%s", controller.OperatorNameLabel, reconciler.GetName()),
fmt.Sprintf("%s=%s", controller.OperatorVersionLabel, reconciler.GetVersion()),
}, ",")).
Do(ctx).
Into(&pods)).
Should(Succeed())
Ω(pods.Items).ShouldNot(HaveLen(0))
*/

result, err := client.Get().
Resource("services").
Namespace(namespacedName.Namespace).
Name(fmt.Sprintf("%s:%d", namespacedName.Name, harbormetav1.HTTPPort)).
SubResource("proxy").
Suffix("/").
DoRaw(ctx)
Ω(err).Should(Succeed())

Ω(html.Parse(bytes.NewReader(result))).
Should(WithTransform(func(node *html.Node) string {
return node.FirstChild.NextSibling.FirstChild.FirstChild.NextSibling.FirstChild.Data
}, Equal("Welcome to ChartMuseum!")))
return health.Healthy
}, BeTrue()))
}
17 changes: 11 additions & 6 deletions controllers/goharbor/chartmuseum/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ package chartmuseum_test

import (
"context"
"path"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/ovh/configstore"

"github.com/goharbor/harbor-operator/controllers"
"github.com/goharbor/harbor-operator/controllers/goharbor/chartmuseum"
Expand All @@ -31,9 +33,10 @@ import (
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var (
stopCh chan struct{}
ctx context.Context
reconciler *chartmuseum.Reconciler
stopCh chan struct{}
ctx context.Context
reconciler *chartmuseum.Reconciler
harborClass string
)

func TestAPIs(t *testing.T) {
Expand All @@ -51,8 +54,10 @@ var _ = BeforeSuite(func(done Done) {

mgr := test.GetManager(ctx)
name := controllers.ChartMuseum.String()
harborClass = test.NewName(name)

configStore := config.NewConfigWithDefaults()
configStore, provider := test.NewConfig(ctx, chartmuseum.ConfigTemplatePathKey, path.Base(chartmuseum.DefaultConfigTemplatePath))
provider.Add(configstore.NewItem(config.HarborClassKey, harborClass, 100))
configStore.Env(name)

commonReconciler, err := chartmuseum.New(ctx, name, configStore)
Expand Down Expand Up @@ -80,7 +85,7 @@ var _ = BeforeSuite(func(done Done) {
}, 60)

var _ = AfterSuite(func() {
close(stopCh)
defer test.AfterSuite(ctx)

test.AfterSuite(ctx)
close(stopCh)
})
66 changes: 41 additions & 25 deletions controllers/goharbor/internal/test/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,62 @@ import (
"math/rand"
"path"

"github.com/goharbor/harbor-operator/pkg/config"
"github.com/onsi/ginkgo"
"github.com/onsi/gomega"
"github.com/ovh/configstore"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func InitSuite() context.Context {
ginkgo.By("Configuring seed")

rand.Seed(ginkgo.GinkgoRandomSeed())
ginkgo.By("Configuring seed", func() {
rand.Seed(ginkgo.GinkgoRandomSeed())
})

ginkgo.By("Configuring logger")
ginkgo.By("Configuring logger", func() {
ConfigureLoggers(ginkgo.GinkgoWriter)
})

ConfigureLoggers(ginkgo.GinkgoWriter)
var ctx context.Context

ctx := NewContext(path.Join("..", "..", ".."))
ginkgo.By("bootstrapping test environment", func() {
ctx = NewContext(path.Join("..", "..", ".."))

ginkgo.By("bootstrapping test environment")
var err error
cfg, err := GetEnvironment(ctx).Start()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(cfg).ToNot(gomega.BeNil())

var err error
cfg, err := GetEnvironment(ctx).Start()
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(cfg).ToNot(gomega.BeNil())
ctx = WithRestConfig(ctx, cfg)

ctx = WithRestConfig(ctx, cfg)
k8sClient, err := client.New(cfg, client.Options{Scheme: GetScheme(ctx)})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(k8sClient).ToNot(gomega.BeNil())

k8sClient, err := client.New(cfg, client.Options{Scheme: GetScheme(ctx)})
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(k8sClient).ToNot(gomega.BeNil())
ctx = WithClient(ctx, k8sClient)

ctx = WithClient(ctx, k8sClient)
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
MetricsBindAddress: "0",
Scheme: GetScheme(ctx),
})
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "failed to create manager")

mgr, err := ctrl.NewManager(cfg, ctrl.Options{
MetricsBindAddress: "0",
Scheme: GetScheme(ctx),
ctx = WithManager(ctx, mgr)
})
gomega.Expect(err).NotTo(gomega.HaveOccurred(), "failed to create manager")

ctx = WithManager(ctx, mgr)
gomega.Expect(ctx).ToNot(gomega.BeNil())

return ctx
}

func AfterSuite(ctx context.Context) {
ginkgo.By("tearing down the test environment")

gomega.Expect(GetEnvironment(ctx).Stop()).
To(gomega.Succeed())
ginkgo.By("tearing down the test environment", func() {
gomega.Expect(GetEnvironment(ctx).Stop()).
To(gomega.Succeed())
})
}

var keepNamespaceOnFailure bool
Expand Down Expand Up @@ -91,3 +97,13 @@ func InitNamespace(ctxFactory func() context.Context) *corev1.Namespace {

return ns
}

const PathToAssets = "../../../config/config/assets"

func NewConfig(ctx context.Context, templateKey, fileName string) (*configstore.Store, *configstore.InMemoryProvider) {
configStore := config.NewConfigWithDefaults()
provider := configStore.InMemory("test")
provider.Add(configstore.NewItem(templateKey, path.Join(PathToAssets, fileName), 100))

return configStore, provider
}
2 changes: 1 addition & 1 deletion controllers/goharbor/internal/test/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func Logs(ctx context.Context, deployment types.NamespacedName) map[string][]byt
return results
}

func LogOnFailureFunc(ctx *context.Context, name func() types.NamespacedName) interface{} {
func LogsAll(ctx *context.Context, name func() types.NamespacedName) interface{} {
return func(done ginkgo.Done) {
defer close(done)

Expand Down
37 changes: 0 additions & 37 deletions pkg/controller/builder.go

This file was deleted.

0 comments on commit 3157529

Please sign in to comment.