Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix docker controller flakes #2839

Merged
merged 2 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions api/repositories/dockercfg/json.go

This file was deleted.

58 changes: 0 additions & 58 deletions api/repositories/dockercfg/json_test.go

This file was deleted.

28 changes: 13 additions & 15 deletions api/repositories/package_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"code.cloudfoundry.org/korifi/api/authorization"
apierrors "code.cloudfoundry.org/korifi/api/errors"
"code.cloudfoundry.org/korifi/api/repositories/conditions"
"code.cloudfoundry.org/korifi/api/repositories/dockercfg"
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/controllers/controllers/shared"
"code.cloudfoundry.org/korifi/controllers/controllers/workloads"
"code.cloudfoundry.org/korifi/tools/dockercfg"
"code.cloudfoundry.org/korifi/tools/k8s"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/uuid"
Expand Down Expand Up @@ -180,28 +180,26 @@ func createImagePullSecret(ctx context.Context, userClient client.Client, cfPack
if err != nil {
return fmt.Errorf("failed to parse image ref: %w", err)
}
dockerCfg, err := dockercfg.GenerateDockerCfgSecretData(*message.Data.Username, *message.Data.Password, ref.Context().RegistryStr())
if err != nil {
return fmt.Errorf("failed to generate dockercfgjson: %w", err)
}

imgPullSecret := corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: cfPackage.Namespace,
Name: cfPackage.Name,
imgPullSecret, err := dockercfg.CreateDockerConfigSecret(
cfPackage.Namespace,
cfPackage.Name,
dockercfg.DockerServerConfig{
Server: ref.Context().RegistryStr(),
Username: *message.Data.Username,
Password: *message.Data.Password,
},
Data: map[string][]byte{
corev1.DockerConfigJsonKey: dockerCfg,
},
Type: corev1.SecretTypeDockerConfigJson,
)
if err != nil {
return fmt.Errorf("failed to generate image pull secret: %w", err)
}

err = controllerutil.SetOwnerReference(cfPackage, &imgPullSecret, scheme.Scheme)
err = controllerutil.SetOwnerReference(cfPackage, imgPullSecret, scheme.Scheme)
if err != nil {
return fmt.Errorf("failed to set ownership from the package to the image pull secret: %w", err)
}

err = userClient.Create(ctx, &imgPullSecret)
err = userClient.Create(ctx, imgPullSecret)
if err != nil {
return fmt.Errorf("failed create the image pull secret: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

//counterfeiter:generate -o fake -fake-name ImageConfigGetter . ImageConfigGetter

type ImageConfigGetter interface {
Config(context.Context, image.Creds, string) (image.Config, error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,53 @@ import (
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
. "code.cloudfoundry.org/korifi/controllers/controllers/workloads/testutils"
"code.cloudfoundry.org/korifi/tools"
"code.cloudfoundry.org/korifi/tools/image"
"code.cloudfoundry.org/korifi/tools/dockercfg"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

v1 "github.com/google/go-containerregistry/pkg/v1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gstruct"
)

var _ = Describe("CFDockerBuildReconciler Integration Tests", func() {
var (
imageSecret *corev1.Secret
imageConfig *v1.ConfigFile
imageRef string

cfSpace *korifiv1alpha1.CFSpace
cfApp *korifiv1alpha1.CFApp
cfPackageGUID string
cfBuild *korifiv1alpha1.CFBuild
)

BeforeEach(func() {
imageConfigGetter.ConfigReturns(image.Config{
Labels: map[string]string{},
ExposedPorts: []int32{},
User: "1000",
}, nil)
imageRef = containerRegistry.ImageRef("foo/bar")
imageConfig = &v1.ConfigFile{
Config: v1.Config{
User: "1000",
},
}

cfSpace = createSpace(cfOrg)

var err error
imageSecret, err = dockercfg.CreateDockerConfigSecret(
cfSpace.Status.GUID,
PrefixedGUID("image-secret"),
dockercfg.DockerServerConfig{
Server: containerRegistry.URL(),
Username: "user",
Password: "password",
},
)
Expect(err).NotTo(HaveOccurred())
Expect(adminClient.Create(ctx, imageSecret)).To(Succeed())

cfApp = &korifiv1alpha1.CFApp{
ObjectMeta: metav1.ObjectMeta{
Name: PrefixedGUID("cf-app"),
Expand Down Expand Up @@ -61,8 +80,8 @@ var _ = Describe("CFDockerBuildReconciler Integration Tests", func() {
},
Source: korifiv1alpha1.PackageSource{
Registry: korifiv1alpha1.Registry{
Image: "some/image",
ImagePullSecrets: []corev1.LocalObjectReference{{Name: "source-image-secret"}},
Image: imageRef,
ImagePullSecrets: []corev1.LocalObjectReference{{Name: imageSecret.Name}},
},
},
},
Expand All @@ -88,6 +107,7 @@ var _ = Describe("CFDockerBuildReconciler Integration Tests", func() {
})

JustBeforeEach(func() {
containerRegistry.PushImage(containerRegistry.ImageRef("foo/bar"), imageConfig)
Expect(adminClient.Create(ctx, cfBuild)).To(Succeed())
})

Expand Down Expand Up @@ -134,21 +154,11 @@ var _ = Describe("CFDockerBuildReconciler Integration Tests", func() {
g.Expect(meta.IsStatusConditionFalse(cfBuild.Status.Conditions, korifiv1alpha1.StagingConditionType)).To(BeTrue())
g.Expect(meta.IsStatusConditionTrue(cfBuild.Status.Conditions, korifiv1alpha1.SucceededConditionType)).To(BeTrue())
g.Expect(cfBuild.Status.Droplet).NotTo(BeNil())
g.Expect(cfBuild.Status.Droplet.Registry.Image).To(Equal("some/image"))
g.Expect(cfBuild.Status.Droplet.Registry.ImagePullSecrets).To(ConsistOf(corev1.LocalObjectReference{Name: "source-image-secret"}))
g.Expect(cfBuild.Status.Droplet.Registry.Image).To(Equal(imageRef))
g.Expect(cfBuild.Status.Droplet.Registry.ImagePullSecrets).To(ConsistOf(corev1.LocalObjectReference{Name: imageSecret.Name}))
}).Should(Succeed())
})

It("fetches the image config", func() {
Expect(imageConfigGetter.ConfigCallCount()).NotTo(BeZero())
_, creds, imageRef := imageConfigGetter.ConfigArgsForCall(imageConfigGetter.ConfigCallCount() - 1)
Expect(imageRef).To(Equal("some/image"))
Expect(creds).To(Equal(image.Creds{
Namespace: cfSpace.Status.GUID,
SecretNames: []string{"source-image-secret"},
}))
})

Describe("privileged images", func() {
succeededCondition := func(g Gomega) metav1.Condition {
g.Expect(adminClient.Get(ctx, client.ObjectKeyFromObject(cfBuild), cfBuild)).To(Succeed())
Expand All @@ -167,7 +177,7 @@ var _ = Describe("CFDockerBuildReconciler Integration Tests", func() {

When("the user is not specified", func() {
BeforeEach(func() {
imageConfigGetter.ConfigReturns(image.Config{}, nil)
imageConfig.Config.User = ""
})

It("fails the build", func() {
Expand All @@ -177,7 +187,7 @@ var _ = Describe("CFDockerBuildReconciler Integration Tests", func() {

When("the user is 'root'", func() {
BeforeEach(func() {
imageConfigGetter.ConfigReturns(image.Config{User: "root"}, nil)
imageConfig.Config.User = "root"
})

It("fails the build", func() {
Expand All @@ -187,7 +197,7 @@ var _ = Describe("CFDockerBuildReconciler Integration Tests", func() {

When("the user is '0'", func() {
BeforeEach(func() {
imageConfigGetter.ConfigReturns(image.Config{User: "0"}, nil)
imageConfig.Config.User = "0"
})

It("fails the build", func() {
Expand All @@ -197,7 +207,7 @@ var _ = Describe("CFDockerBuildReconciler Integration Tests", func() {

When("the user is 'root:rootgroup'", func() {
BeforeEach(func() {
imageConfigGetter.ConfigReturns(image.Config{User: "root:rootgroup"}, nil)
imageConfig.Config.User = "root:rootgroup"
})

It("fails the build", func() {
Expand All @@ -207,7 +217,7 @@ var _ = Describe("CFDockerBuildReconciler Integration Tests", func() {

When("the user is '0:rootgroup'", func() {
BeforeEach(func() {
imageConfigGetter.ConfigReturns(image.Config{User: "0:rootgroup"}, nil)
imageConfig.Config.User = "0:rootgroup"
})

It("fails the build", func() {
Expand Down
Loading