Skip to content

Commit

Permalink
Simplify dockerfiles
Browse files Browse the repository at this point in the history
Leverage dockerignore to reduce the explicit `COPY` statements in
dockerfiles

Co-authored-by: Georgi Sabev <georgethebeatle@gmail.com>
  • Loading branch information
danail-branekov and georgethebeatle committed Jul 19, 2024
1 parent a16cd9a commit c376fb7
Show file tree
Hide file tree
Showing 23 changed files with 130 additions and 139 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
!**/*.go
!**/*.mod
!**/*.sum

**/fake
**/*_test.go
15 changes: 1 addition & 14 deletions api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,12 @@ COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download

COPY api api
COPY model model
COPY api/actions api/actions
COPY api/errors api/errors
COPY api/authorization api/authorization
COPY api/config/config.go api/config/config.go
COPY api/handlers api/handlers
COPY api/main.go api/main.go
COPY api/middleware api/middleware
COPY api/payloads api/payloads
COPY api/presenter api/presenter
COPY api/repositories api/repositories
COPY api/routing api/routing
COPY api/tools api/tools
COPY controllers/api controllers/api
COPY controllers/config controllers/config
COPY controllers/controllers/shared controllers/controllers/shared
COPY controllers/controllers/workloads controllers/controllers/workloads
COPY controllers/controllers/services/credentials controllers/controllers/services/credentials
COPY controllers/controllers/services/brokers controllers/controllers/services/brokers
COPY controllers/webhooks controllers/webhooks
COPY tools tools
COPY version version
Expand Down
15 changes: 3 additions & 12 deletions api/remote-debug/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,12 @@ COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download

COPY api/actions api/actions
COPY api/errors api/errors
COPY api/authorization api/authorization
COPY api/config/config.go api/config/config.go
COPY api/handlers api/handlers
COPY api/main.go api/main.go
COPY api/middleware api/middleware
COPY api/payloads api/payloads
COPY api/presenter api/presenter
COPY api/repositories api/repositories
COPY api/routing api/routing
COPY api api
COPY model model
COPY controllers/api controllers/api
COPY controllers/config controllers/config
COPY controllers/controllers/shared controllers/controllers/shared
COPY controllers/controllers/workloads controllers/controllers/workloads
COPY controllers/controllers/services/credentials controllers/controllers/services/credentials
COPY controllers/webhooks controllers/webhooks
COPY tools tools
COPY version version
Expand Down
4 changes: 2 additions & 2 deletions api/repositories/service_broker_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"code.cloudfoundry.org/korifi/api/authorization"
apierrors "code.cloudfoundry.org/korifi/api/errors"
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/controllers/controllers/services/credentials"
"code.cloudfoundry.org/korifi/model"
"code.cloudfoundry.org/korifi/model/services"
"code.cloudfoundry.org/korifi/tools"
"github.com/BooleanCat/go-functional/iter"
"github.com/google/uuid"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -68,7 +68,7 @@ func (r *ServiceBrokerRepo) CreateServiceBroker(ctx context.Context, authInfo au
return ServiceBrokerResource{}, fmt.Errorf("failed to build user client: %w", err)
}

credsSecretData, err := credentials.ToCredentialsSecretData(message.Credentials)
credsSecretData, err := tools.ToCredentialsSecretData(message.Credentials)
if err != nil {
return ServiceBrokerResource{}, fmt.Errorf("failed to create credentials secret data: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion api/repositories/service_broker_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"code.cloudfoundry.org/korifi/model"
"code.cloudfoundry.org/korifi/model/services"
"code.cloudfoundry.org/korifi/tests/matchers"
"code.cloudfoundry.org/korifi/tools"
"code.cloudfoundry.org/korifi/tools/k8s"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -133,7 +134,7 @@ var _ = Describe("ServiceBrokerRepo", func() {
Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(credentialsSecret), credentialsSecret)).To(Succeed())
Expect(credentialsSecret.Data).To(SatisfyAll(
HaveLen(1),
HaveKeyWithValue(korifiv1alpha1.CredentialsSecretKey,
HaveKeyWithValue(tools.CredentialsSecretKey,
MatchJSON(`{"username" : "broker-user", "password": "broker-password"}`),
),
))
Expand Down
4 changes: 2 additions & 2 deletions api/repositories/service_instance_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"code.cloudfoundry.org/korifi/api/authorization"
apierrors "code.cloudfoundry.org/korifi/api/errors"
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/controllers/controllers/services/credentials"
"code.cloudfoundry.org/korifi/tools"
"code.cloudfoundry.org/korifi/tools/k8s"

"github.com/BooleanCat/go-functional/iter"
Expand Down Expand Up @@ -226,7 +226,7 @@ func (r *ServiceInstanceRepo) createCredentialsSecret(
credentialsSecret.Labels[CFServiceInstanceGUIDLabel] = cfServiceInstance.Name

var err error
credentialsSecret.Data, err = credentials.ToCredentialsSecretData(creds)
credentialsSecret.Data, err = tools.ToCredentialsSecretData(creds)
if err != nil {
return errors.New("failed to marshal credentials for service instance")
}
Expand Down
16 changes: 8 additions & 8 deletions api/repositories/service_instance_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ var _ = Describe("ServiceInstanceRepository", func() {

It("creates the credentials secret", func() {
Expect(createdSecret.Type).To(Equal(corev1.SecretTypeOpaque))
Expect(createdSecret.Data).To(MatchAllKeys(Keys{korifiv1alpha1.CredentialsSecretKey: Not(BeEmpty())}))
Expect(createdSecret.Data).To(MatchAllKeys(Keys{tools.CredentialsSecretKey: Not(BeEmpty())}))
credentials := map[string]any{}
Expect(json.Unmarshal(createdSecret.Data[korifiv1alpha1.CredentialsSecretKey], &credentials)).To(Succeed())
Expect(json.Unmarshal(createdSecret.Data[tools.CredentialsSecretKey], &credentials)).To(Succeed())
Expect(credentials).To(Equal(serviceInstanceCredentials))
})
})
Expand Down Expand Up @@ -144,7 +144,7 @@ var _ = Describe("ServiceInstanceRepository", func() {
Namespace: space.Name,
},
StringData: map[string]string{
korifiv1alpha1.CredentialsSecretKey: `{"a": "b"}`,
tools.CredentialsSecretKey: `{"a": "b"}`,
},
}
Expect(k8sClient.Create(ctx, secret)).To(Succeed())
Expand Down Expand Up @@ -236,9 +236,9 @@ var _ = Describe("ServiceInstanceRepository", func() {
It("does not change the credential secret", func() {
Consistently(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(secret), secret)).To(Succeed())
g.Expect(secret.Data).To(MatchAllKeys(Keys{korifiv1alpha1.CredentialsSecretKey: Not(BeEmpty())}))
g.Expect(secret.Data).To(MatchAllKeys(Keys{tools.CredentialsSecretKey: Not(BeEmpty())}))
credentials := map[string]any{}
g.Expect(json.Unmarshal(secret.Data[korifiv1alpha1.CredentialsSecretKey], &credentials)).To(Succeed())
g.Expect(json.Unmarshal(secret.Data[tools.CredentialsSecretKey], &credentials)).To(Succeed())
g.Expect(credentials).To(MatchAllKeys(Keys{
"a": Equal("b"),
}))
Expand All @@ -264,9 +264,9 @@ var _ = Describe("ServiceInstanceRepository", func() {
Expect(err).NotTo(HaveOccurred())
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(secret), secret)).To(Succeed())
g.Expect(secret.Data).To(MatchAllKeys(Keys{korifiv1alpha1.CredentialsSecretKey: Not(BeEmpty())}))
g.Expect(secret.Data).To(MatchAllKeys(Keys{tools.CredentialsSecretKey: Not(BeEmpty())}))
credentials := map[string]any{}
Expect(json.Unmarshal(secret.Data[korifiv1alpha1.CredentialsSecretKey], &credentials)).To(Succeed())
Expect(json.Unmarshal(secret.Data[tools.CredentialsSecretKey], &credentials)).To(Succeed())
Expect(credentials).To(MatchAllKeys(Keys{
"object": MatchAllKeys(Keys{"c": Equal("d")}),
}))
Expand All @@ -291,7 +291,7 @@ var _ = Describe("ServiceInstanceRepository", func() {
Name: "foo",
},
Data: map[string][]byte{
korifiv1alpha1.CredentialsSecretKey: []byte(`{"type":"database"}`),
tools.CredentialsSecretKey: []byte(`{"type":"database"}`),
},
})).To(Succeed())
Expect(k8s.Patch(ctx, k8sClient, cfServiceInstance, func() {
Expand Down
20 changes: 4 additions & 16 deletions controllers/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,10 @@ RUN --mount=type=cache,target=/go/pkg/mod \
go mod download

COPY model model
COPY controllers/api controllers/api
COPY controllers/config/config.go controllers/config/config.go
COPY controllers/controllers controllers/controllers
COPY controllers/cleanup controllers/cleanup
COPY controllers/coordination controllers/coordination
COPY controllers/main.go controllers/main.go
COPY controllers/webhooks controllers/webhooks

COPY kpack-image-builder/controllers/ kpack-image-builder/controllers

COPY job-task-runner/api job-task-runner/api
COPY job-task-runner/controllers job-task-runner/controllers

COPY statefulset-runner/api/ statefulset-runner/api
COPY statefulset-runner/controllers/ statefulset-runner/controllers

COPY controllers controllers
COPY kpack-image-builder kpack-image-builder
COPY job-task-runner job-task-runner
COPY statefulset-runner statefulset-runner
COPY tools tools
COPY version version

Expand Down
2 changes: 0 additions & 2 deletions controllers/api/v1alpha1/shared_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ const (
PropagateDeletionAnnotation = "cloudfoundry.org/propagate-deletion"
PropagatedFromLabel = "cloudfoundry.org/propagated-from"

CredentialsSecretKey = "credentials"

RelationshipsLabelPrefix = "korifi.cloudfoundry.org/rel-"
RelServiceBrokerLabel = RelationshipsLabelPrefix + "service_broker"
RelServiceOfferingLabel = RelationshipsLabelPrefix + "service_offering"
Expand Down
6 changes: 3 additions & 3 deletions controllers/controllers/services/bindings/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var _ = Describe("CFServiceBinding", func() {
Namespace: testNamespace,
},
Data: map[string][]byte{
korifiv1alpha1.CredentialsSecretKey: credentialsBytes,
tools.CredentialsSecretKey: credentialsBytes,
},
}

Expand Down Expand Up @@ -375,7 +375,7 @@ var _ = Describe("CFServiceBinding", func() {
Namespace: testNamespace,
},
Data: map[string][]byte{
korifiv1alpha1.CredentialsSecretKey: credentialsBytes,
tools.CredentialsSecretKey: credentialsBytes,
},
}

Expand Down Expand Up @@ -431,7 +431,7 @@ var _ = Describe("CFServiceBinding", func() {
Namespace: testNamespace,
},
Data: map[string][]byte{
korifiv1alpha1.CredentialsSecretKey: credentialsBytes,
tools.CredentialsSecretKey: credentialsBytes,
},
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/controllers/services/brokers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (r *Reconciler) ReconcileResource(ctx context.Context, cfServiceBroker *kor

func (r *Reconciler) validateCredentials(credentialsSecret *corev1.Secret) error {
creds := map[string]any{}
err := json.Unmarshal(credentialsSecret.Data[korifiv1alpha1.CredentialsSecretKey], &creds)
err := json.Unmarshal(credentialsSecret.Data[tools.CredentialsSecretKey], &creds)
if err != nil {
return fmt.Errorf("invalid credentials secret %q: %w", credentialsSecret.Name, err)
}
Expand Down
9 changes: 5 additions & 4 deletions controllers/controllers/services/brokers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"code.cloudfoundry.org/korifi/model/services"
"code.cloudfoundry.org/korifi/tests/helpers/broker"
. "code.cloudfoundry.org/korifi/tests/matchers"
"code.cloudfoundry.org/korifi/tools"
"code.cloudfoundry.org/korifi/tools/k8s"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -98,7 +99,7 @@ var _ = Describe("CFServiceBroker", func() {
Namespace: testNamespace,
},
Data: map[string][]byte{
korifiv1alpha1.CredentialsSecretKey: []byte(`{"username": "broker-user", "password": "broker-password"}`),
tools.CredentialsSecretKey: []byte(`{"username": "broker-user", "password": "broker-password"}`),
},
}
Expect(adminClient.Create(ctx, credentialsSecret)).To(Succeed())
Expand Down Expand Up @@ -316,7 +317,7 @@ var _ = Describe("CFServiceBroker", func() {
Namespace: testNamespace,
},
Data: map[string][]byte{
korifiv1alpha1.CredentialsSecretKey: []byte(`{"username": "broker-user", "password": "broker-password"}`),
tools.CredentialsSecretKey: []byte(`{"username": "broker-user", "password": "broker-password"}`),
},
}
Expect(adminClient.Create(ctx, credentialsSecret)).To(Succeed())
Expand Down Expand Up @@ -365,7 +366,7 @@ var _ = Describe("CFServiceBroker", func() {
BeforeEach(func() {
Expect(k8s.PatchResource(ctx, adminClient, credentialsSecret, func() {
credentialsSecret.Data = map[string][]byte{
korifiv1alpha1.CredentialsSecretKey: []byte(`{ "password": "broker-password"}`),
tools.CredentialsSecretKey: []byte(`{ "password": "broker-password"}`),
}
})).To(Succeed())
})
Expand All @@ -386,7 +387,7 @@ var _ = Describe("CFServiceBroker", func() {
BeforeEach(func() {
Expect(k8s.PatchResource(ctx, adminClient, credentialsSecret, func() {
credentialsSecret.Data = map[string][]byte{
korifiv1alpha1.CredentialsSecretKey: []byte(`{ "username": "broker-username"}`),
tools.CredentialsSecretKey: []byte(`{ "username": "broker-username"}`),
}
})).To(Succeed())
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"code.cloudfoundry.org/korifi/model/services"
"code.cloudfoundry.org/korifi/tests/helpers"
"code.cloudfoundry.org/korifi/tests/helpers/broker"
"code.cloudfoundry.org/korifi/tools"
"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -45,7 +46,7 @@ var _ = Describe("OSBAPI Client", func() {
Name: uuid.NewString(),
},
Data: map[string][]byte{
korifiv1alpha1.CredentialsSecretKey: []byte(`{"username":"broker-user","password":"broker-password"}`),
tools.CredentialsSecretKey: []byte(`{"username":"broker-user","password":"broker-password"}`),
},
}
helpers.EnsureCreate(adminClient, creds)
Expand Down
18 changes: 3 additions & 15 deletions controllers/controllers/services/credentials/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package credentials

import (
"encoding/json"
"errors"
"fmt"

korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/tools"
corev1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -55,12 +55,12 @@ func toBytes(value any) ([]byte, error) {
}

func GetCredentials(credentialsSecret *corev1.Secret) (map[string]any, error) {
credentials, ok := credentialsSecret.Data[korifiv1alpha1.CredentialsSecretKey]
credentials, ok := credentialsSecret.Data[tools.CredentialsSecretKey]
if !ok {
return nil, fmt.Errorf(
"data of secret %q does not contain the %q key",
credentialsSecret.Name,
korifiv1alpha1.CredentialsSecretKey,
tools.CredentialsSecretKey,
)
}
credentialsObject := map[string]any{}
Expand All @@ -71,15 +71,3 @@ func GetCredentials(credentialsSecret *corev1.Secret) (map[string]any, error) {

return credentialsObject, nil
}

func ToCredentialsSecretData(credentials any) (map[string][]byte, error) {
var credentialBytes []byte
credentialBytes, err := json.Marshal(credentials)
if err != nil {
return nil, errors.New("failed to marshal credentials for service instance")
}

return map[string][]byte{
korifiv1alpha1.CredentialsSecretKey: credentialBytes,
}, nil
}
Loading

0 comments on commit c376fb7

Please sign in to comment.