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

Read PostgreSQL admin password from connection secret #284

Merged
merged 2 commits into from
Aug 19, 2021
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:

env:
# Common versions
GO_VERSION: '1.14'
GO_VERSION: '1.16'
GOLANGCI_VERSION: 'v1.31'
DOCKER_BUILDX_VERSION: 'v0.4.2'

Expand Down
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ PROJECT_NAME := provider-azure
PROJECT_REPO := github.com/crossplane/$(PROJECT_NAME)

PLATFORMS ?= linux_amd64 linux_arm64

# kind-related versions
KIND_VERSION ?= v0.11.1
KIND_NODE_IMAGE_TAG ?= v1.19.11
# -include will silently skip missing files, which allows us
# to load those files with a target in the Makefile. If only
# "include" was used, the make command would fail and refuse
Expand Down Expand Up @@ -92,7 +96,7 @@ e2e.run: test-integration
# Run integration tests.
test-integration: $(KIND) $(KUBECTL) $(HELM3)
@$(INFO) running integration tests using kind $(KIND_VERSION)
@$(ROOT_DIR)/cluster/local/integration_tests.sh || $(FAIL)
@KIND_NODE_IMAGE_TAG=${KIND_NODE_IMAGE_TAG} KIND_VERSION=${KIND_VERSION} $(ROOT_DIR)/cluster/local/integration_tests.sh || $(FAIL)
@$(OK) integration tests passed

# Update the submodules, such as the common build scripts.
Expand All @@ -115,6 +119,8 @@ manifests:
# using unit tests.
KUBEBUILDER_VERSION ?= 1.0.8
KUBEBUILDER := $(TOOLS_HOST_DIR)/kubebuilder-$(KUBEBUILDER_VERSION)
KUBEBUILDER_OS ?= $(GOHOSTOS)
KUBEBUILDER_ARCH ?= $(GOHOSTARCH)
TEST_ASSET_KUBE_APISERVER := $(KUBEBUILDER)/kube-apiserver
TEST_ASSET_ETCD := $(KUBEBUILDER)/etcd
export TEST_ASSET_KUBE_APISERVER TEST_ASSET_ETCD
Expand Down Expand Up @@ -150,7 +156,7 @@ help-special: crossplane.help
$(KUBEBUILDER):
@$(INFO) installing kubebuilder $(KUBEBUILDER_VERSION)
@mkdir -p $(TOOLS_HOST_DIR)/tmp || $(FAIL)
@curl -fsSL https://github.com/kubernetes-sigs/kubebuilder/releases/download/v$(KUBEBUILDER_VERSION)/kubebuilder_$(KUBEBUILDER_VERSION)_$(GOHOSTOS)_$(GOHOSTARCH).tar.gz | tar -xz -C $(TOOLS_HOST_DIR)/tmp || $(FAIL)
@mv $(TOOLS_HOST_DIR)/tmp/kubebuilder_$(KUBEBUILDER_VERSION)_$(GOHOSTOS)_$(GOHOSTARCH)/bin $(KUBEBUILDER) || $(FAIL)
@curl -fsSL https://github.com/kubernetes-sigs/kubebuilder/releases/download/v$(KUBEBUILDER_VERSION)/kubebuilder_$(KUBEBUILDER_VERSION)_$(KUBEBUILDER_OS)_$(KUBEBUILDER_ARCH).tar.gz | tar -xz -C $(TOOLS_HOST_DIR)/tmp || $(FAIL)
@mv $(TOOLS_HOST_DIR)/tmp/kubebuilder_$(KUBEBUILDER_VERSION)_$(KUBEBUILDER_OS)_$(KUBEBUILDER_ARCH)/bin $(KUBEBUILDER) || $(FAIL)
@rm -fr $(TOOLS_HOST_DIR)/tmp
@$(OK) installing kubebuilder $(KUBEBUILDER_VERSION)
5 changes: 3 additions & 2 deletions cluster/local/integration_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ echo "created cache dir at ${CACHE_PATH}"
docker save "${BUILD_IMAGE}" -o "${CACHE_PATH}/${PACKAGE_NAME}.xpkg" && chmod 644 "${CACHE_PATH}/${PACKAGE_NAME}.xpkg"

# create kind cluster with extra mounts
echo_step "creating k8s cluster using kind"
KIND_NODE_IMAGE="kindest/node:${KIND_NODE_IMAGE_TAG}"
echo_step "creating k8s cluster using kind ${KIND_VERSION} and node image ${KIND_NODE_IMAGE}"
KIND_CONFIG="$( cat <<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
Expand All @@ -82,7 +83,7 @@ nodes:
containerPath: /cache
EOF
)"
echo "${KIND_CONFIG}" | "${KIND}" create cluster --name="${K8S_CLUSTER}" --wait=5m --config=-
echo "${KIND_CONFIG}" | "${KIND}" create cluster --name="${K8S_CLUSTER}" --wait=5m --image="${KIND_NODE_IMAGE}" --config=-

# tag controller image and load it into kind cluster
docker tag "${CONTROLLER_IMAGE}" "${PACKAGE_CONTROLLER_IMAGE}"
Expand Down
30 changes: 28 additions & 2 deletions pkg/controller/database/postgresqlserver/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2017-12-01/postgresql"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/workqueue"
"sigs.k8s.io/controller-runtime/pkg/controller"

Expand Down Expand Up @@ -52,6 +54,7 @@ const (
errGetPostgreSQLServer = "cannot get PostgreSQLServer"
errDeletePostgreSQLServer = "cannot delete PostgreSQLServer"
errFetchLastOperation = "cannot fetch last operation"
errGetConnSecret = "cannot get connection secret"
)

// Setup adds a controller that reconciles PostgreSQLInstances.
Expand Down Expand Up @@ -146,6 +149,23 @@ func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return o, nil
}

func (e *external) getPassword(ctx context.Context, cr *v1beta1.PostgreSQLServer) (string, error) {
if cr.Spec.WriteConnectionSecretToReference == nil ||
cr.Spec.WriteConnectionSecretToReference.Name == "" || cr.Spec.WriteConnectionSecretToReference.Namespace == "" {
return "", nil
}

s := &v1.Secret{}
if err := e.kube.Get(ctx, types.NamespacedName{
Namespace: cr.Spec.WriteConnectionSecretToReference.Namespace,
Name: cr.Spec.WriteConnectionSecretToReference.Name,
}, s); err != nil {
return "", errors.Wrap(err, errGetConnSecret)
}

return string(s.Data[xpv1.ResourceCredentialsSecretPasswordKey]), nil
}

func (e *external) Create(ctx context.Context, mg resource.Managed) (managed.ExternalCreation, error) {
cr, ok := mg.(*v1beta1.PostgreSQLServer)
if !ok {
Expand All @@ -154,9 +174,15 @@ func (e *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext

cr.SetConditions(xpv1.Creating())

pw, err := e.newPasswordFn()
pw, err := e.getPassword(ctx, cr)
if err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, errGenPassword)
return managed.ExternalCreation{}, err
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought you said it'd save the password to the connection secret before triggering the creation. Is that not the case anymore?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, it was never the case (also not the case when I ran the above set of experiments). We still leave it to the managed reconciler.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. So, we save the password after a successful CreateServer call via the managed reconciler and if CreateServer is ever called again, we'd be getting the password from connection secret.

if pw == "" {
pw, err = e.newPasswordFn()
if err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, errGenPassword)
}
}
if err := e.client.CreateServer(ctx, cr, pw); err != nil {
return managed.ExternalCreation{}, errors.Wrap(err, errCreatePostgreSQLServer)
Expand Down