Skip to content

test: platform secrets as sealed secrets (PoC)#2956

Draft
ferruhcihan wants to merge 49 commits intomainfrom
APL-1592
Draft

test: platform secrets as sealed secrets (PoC)#2956
ferruhcihan wants to merge 49 commits intomainfrom
APL-1592

Conversation

@ferruhcihan
Copy link
Collaborator

📌 Summary

🔍 Reviewer Notes

🧹 Checklist

  • Code is readable, maintainable, and robust.
  • Unit tests added/updated

envFrom:
- secretRef:
name: {{ include "otomi-api.fullname" . }}
{{- with .Values.existingSecret }}
Copy link
Contributor

Choose a reason for hiding this comment

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

please add default value for the existingSecret to the values.yaml of this chart.

Copy link
Contributor

@CasLubbers CasLubbers left a comment

Choose a reason for hiding this comment

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

Really a lot of work impressive! The general idea is good. I have placed some comments on all the kubectl commands. We should not use those. Otherwise we can run in to issues during the apply.
And one thing I really disliked is the hardcoded mapping in the sealed-secrets.ts. If you want we can maybe think of something better together.
And you will have some merge conflicts. I moved all of the git stuff in to one place.

// the bootstrap phase before install) may have set the URL with unresolved placeholder
// passwords because K8s secrets didn't exist yet. Now that secrets are decrypted,
// we need to update the URL with the real credentials.
cd(env.ENV_DIR)
Copy link
Contributor

Choose a reason for hiding this comment

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

We should try to avoid changing directories in code. With zx you can specify a directory the code should execute on:

await $`git remote set-url origin ${remote}`
  .cwd(env.ENV_DIR)
  .nothrow()
  .quiet()

d.info(`Waiting for ${secretsToWait.size} sealed secrets to be decrypted`)
const start = Date.now()

while (Date.now() - start < timeoutMs) {
Copy link
Contributor

Choose a reason for hiding this comment

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

To be more aligned with the rest of this repository you could use the async retry library. And then you throw an error if pending.length is not null

labels:
name: ${namespace}`

await deps.$`echo ${nsYaml} | kubectl apply -f -`.nothrow().quiet()
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not use kubectl apply anymore. Only use the kubernetes/client-node library.

* Ensure a namespace exists. If it doesn't exist, create it with proper labels.
* This avoids overwriting labels on existing namespaces that were created by k8s-raw.gotmpl.
*/
export const ensureNamespaceExists = async (namespace: string, deps = { $, terminal }): Promise<void> => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a function I would not expect to exist in the sealed-secrets.ts
I would move this one to the k8s.ts

* Mapping from secret path prefix to target Kubernetes namespace.
* Dynamic entries like `teamConfig.{teamId}` are handled separately.
*/
export const APP_NAMESPACE_MAP: Record<string, string> = {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would really try to avoid such hardcoded maps in code. It's really prone to human error.
Is this something we can maybe specify in the values-schema?

const d = deps.terminal(`common:${cmdName}:restartSealedSecretsController`)
d.info('Restarting sealed-secrets controller to ensure correct key is used')

const result = await deps.$`kubectl rollout restart deployment/sealed-secrets -n sealed-secrets`.nothrow().quiet()
Copy link
Contributor

Choose a reason for hiding this comment

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

You can replace this with the kubernetes/client-node library by patching deployment annotation kubectl.kubernetes.io/restartedAt

}

d.info('Waiting for sealed-secrets controller rollout')
const waitResult = await deps.$`kubectl rollout status deployment/sealed-secrets -n sealed-secrets --timeout=120s`
Copy link
Contributor

Choose a reason for hiding this comment

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

You can replace this with the kubernetes/client-node library by patching deployment annotation kubectl.kubernetes.io/restartedAt

private async verifyGitRepoHasMainBranch(): Promise<boolean> {
try {
// Get credentials from K8s secret (created by Helm at deploy time)
const creds = await getK8sSecret('gitea-credentials', 'apl-operator')
Copy link
Contributor

Choose a reason for hiding this comment

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

To get the git config please use the functions in git-config.ts getStoredGitRepoConfig

Copy link
Contributor

Choose a reason for hiding this comment

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

Otherwise this will not work for BYO Git.

branch: string
}

export const getRepo = async (values: Record<string, any>, deps = { getK8sSecret, terminal }): Promise<Repo> => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you need to update your branch. I removed this function

const otomiGit = values?.otomi?.git
const agePrivateKey = values?.kms?.sops?.age?.privateKey
this.d.debug('Reading git credentials from K8s Secret')
const giteaSecrets = await getK8sSecret('gitea-secrets', 'sealed-secrets')
Copy link
Contributor

Choose a reason for hiding this comment

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

I moved all the code for retrieving git data to: git-config.ts

pipeline: otomi-task-teams
values:
- ../values/team-ns/team-ns.gotmpl
- name: team-secrets-{{ $teamId }}
Copy link
Contributor

Choose a reason for hiding this comment

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

This could be a part of team-ns chart right?

const result = await $`git ls-remote --exit-code --heads ${repoUrl} main`.nothrow().quiet()
return result.exitCode === 0
} catch {
// If we can't check (e.g. gitea not ready yet), assume it's fine
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not entirely convinced. What if user provides incorrect credentials?

* Ensure a namespace exists. If it doesn't exist, create it with proper labels.
* This avoids overwriting labels on existing namespaces that were created by k8s-raw.gotmpl.
*/
export const ensureNamespaceExists = async (namespace: string, deps = { $, terminal }): Promise<void> => {
Copy link
Contributor

Choose a reason for hiding this comment

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

consider using the k8s library with patch that can eiter created or update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Comments