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

[RFC-0003] Add commands for managing OCI artifacts #2856

Merged
merged 33 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
e927d39
Add OCI internal package
stefanprodan Jun 21, 2022
9503eca
Add artifact commands
stefanprodan Jun 21, 2022
12a491f
Update controllers to OCI preview images
stefanprodan Jun 22, 2022
2d8db4f
Implement OCIRepository commands
stefanprodan Jun 22, 2022
2f35367
Add list artifacts command
stefanprodan Jun 23, 2022
e4fb8e7
Add e2e tests for artifact commands
stefanprodan Jun 23, 2022
b6a78f4
Update SC with OCI semver support
stefanprodan Jun 23, 2022
30e5389
Run e2e tests for PRs against oci branch
stefanprodan Jul 1, 2022
adc7981
Add tests for source oci command
somtochiama Jun 30, 2022
8049634
Add `oci://` prefix
stefanprodan Jul 6, 2022
6b98590
Add `--cert-ref` to `flux create source oci`
stefanprodan Jul 8, 2022
7dd7369
Use OCI standard annotations
stefanprodan Jul 11, 2022
1b327e9
Show artifact digest in list output
stefanprodan Jul 11, 2022
3e15e83
Add test for tag/list/build/pull/push artifacts
somtochiama Jul 8, 2022
b78bbd5
fill test files
somtochiama Jul 8, 2022
9e76787
working golden files
somtochiama Jul 11, 2022
009413a
Add test for annotations
somtochiama Jul 11, 2022
030b6bc
Update source-controller with OCI metadata
stefanprodan Jul 13, 2022
8031045
Add `make build-dev` command
stefanprodan Jul 13, 2022
70d30fd
Update golden files to latest digest
stefanprodan Jul 13, 2022
4c576bf
Add create oci secret command
somtochiama Jul 18, 2022
fe4b659
Update cli description
somtochiama Jul 19, 2022
fcd38c9
Fix cli description
somtochiama Jul 19, 2022
41aac68
Add link to kubectl repo
somtochiama Jul 19, 2022
69e26ca
Pull artifact not push artifact
Aug 2, 2022
08401f6
Add OCI provider arg
stefanprodan Aug 3, 2022
7c7e76f
Use fluxcd/pkg/oci/client
stefanprodan Aug 3, 2022
ac9b3d1
Update controllers with OCI support
stefanprodan Aug 8, 2022
d4718f6
Improve artifact commands docs
stefanprodan Aug 9, 2022
d4c5a13
Add examples for pushing artifacts with GH Actions
stefanprodan Aug 9, 2022
75a879c
OCI docs improvements
stefanprodan Aug 10, 2022
b810aea
Make `flux trace` work with OCIRepository
makkes Aug 10, 2022
7e2d235
Merge pull request #2971 from fluxcd/trace-ocirepo
makkes Aug 10, 2022
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
37 changes: 36 additions & 1 deletion .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
branches: [ main, oci ]

jobs:
kind:
runs-on: ubuntu-latest
services:
registry:
image: registry:2
ports:
- 5000:5000
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -168,6 +173,36 @@ jobs:
- name: flux delete source git
run: |
/tmp/flux delete source git podinfo --silent
- name: flux oci artifacts
run: |
/tmp/flux push artifact oci://localhost:5000/fluxcd/flux:${{ github.sha }} \
--path="./manifests" \
--source="${{ github.repositoryUrl }}" \
--revision="${{ github.ref }}/${{ github.sha }}"
/tmp/flux tag artifact oci://localhost:5000/fluxcd/flux:${{ github.sha }} \
--tag latest
/tmp/flux list artifacts oci://localhost:5000/fluxcd/flux
- name: flux oci repositories
run: |
/tmp/flux create source oci podinfo-oci \
--url oci://ghcr.io/stefanprodan/manifests/podinfo \
--tag-semver 6.1.x \
--interval 10m
/tmp/flux create kustomization podinfo-oci \
--source=OCIRepository/podinfo-oci \
--path="./kustomize" \
--prune=true \
--interval=5m \
--target-namespace=default \
--wait=true \
--health-check-timeout=3m
/tmp/flux reconcile source oci podinfo-oci
/tmp/flux suspend source oci podinfo-oci
/tmp/flux get sources oci
/tmp/flux resume source oci podinfo-oci
/tmp/flux export source oci podinfo-oci
/tmp/flux delete ks podinfo-oci --silent
/tmp/flux delete source oci podinfo-oci --silent
- name: flux create tenant
run: |
/tmp/flux create tenant dev-team --with-namespace=apps
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
VERSION?=$(shell grep 'VERSION' cmd/flux/main.go | awk '{ print $$4 }' | head -n 1 | tr -d '"')
DEV_VERSION?=0.0.0-$(shell git rev-parse --abbrev-ref HEAD)-$(shell git rev-parse --short HEAD)-$(shell date +%s)
EMBEDDED_MANIFESTS_TARGET=cmd/flux/.manifests.done
TEST_KUBECONFIG?=/tmp/flux-e2e-test-kubeconfig
# Architecture to use envtest with
Expand Down Expand Up @@ -55,6 +56,9 @@ $(EMBEDDED_MANIFESTS_TARGET): $(call rwildcard,manifests/,*.yaml *.json)
build: $(EMBEDDED_MANIFESTS_TARGET)
CGO_ENABLED=0 go build -ldflags="-s -w -X main.VERSION=$(VERSION)" -o ./bin/flux ./cmd/flux

build-dev: $(EMBEDDED_MANIFESTS_TARGET)
CGO_ENABLED=0 go build -ldflags="-s -w -X main.VERSION=$(DEV_VERSION)" -o ./bin/flux ./cmd/flux

.PHONY: install
install:
CGO_ENABLED=0 go install ./cmd/flux
Expand Down
82 changes: 81 additions & 1 deletion action/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ You can download a specific version with:
- name: Setup Flux CLI
uses: fluxcd/flux2/action@main
with:
version: 0.8.0
version: 0.32.0
```

### Automate Flux updates
Expand Down Expand Up @@ -74,6 +74,86 @@ jobs:
${{ steps.update.outputs.flux_version }}
```

### Push Kubernetes manifests to container registries

Example workflow for publishing Kubernetes manifests bundled as OCI artifacts to GitHub Container Registry:

```yaml
name: push-artifact-staging

on:
push:
branches:
- 'main'

permissions:
packages: write # needed for ghcr.io access

env:
OCI_REPO: "oci://ghcr.io/my-org/manifests/${{ github.event.repository.name }}"
stefanprodan marked this conversation as resolved.
Show resolved Hide resolved

jobs:
kubernetes:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
stefanprodan marked this conversation as resolved.
Show resolved Hide resolved
- name: Setup Flux CLI
uses: fluxcd/flux2/action@main
- name: Generate manifests
run: |
kustomize build ./manifests/staging > ./deploy/app.yaml
stefanprodan marked this conversation as resolved.
Show resolved Hide resolved
- name: Push manifests
run: |
flux push artifact $OCI_REPO:$(git rev-parse --short HEAD) \
--path="./deploy" \
--source="$(git config --get remote.origin.url)" \
--revision="$(git branch --show-current)/$(git rev-parse HEAD)"
- name: Deploy manifests to staging
run: |
flux tag artifact $OCI_REPO:$(git rev-parse --short HEAD) --tag staging
```

Example workflow for publishing Kubernetes manifests bundled as OCI artifacts to Docker Hub:

```yaml
name: push-artifact-production

on:
push:
tags:
- '*'

env:
OCI_REPO: "oci://docker.io/my-org/app-config"

jobs:
kubernetes:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
stefanprodan marked this conversation as resolved.
Show resolved Hide resolved
- name: Setup Flux CLI
uses: fluxcd/flux2/action@main
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
stefanprodan marked this conversation as resolved.
Show resolved Hide resolved
- name: Generate manifests
run: |
kustomize build ./manifests/production > ./deploy/app.yaml
- name: Push manifests
run: |
flux push artifact $OCI_REPO:$(git tag --points-at HEAD) \
--path="./deploy" \
--source="$(git config --get remote.origin.url)" \
--revision="$(git tag --points-at HEAD)/$(git rev-parse HEAD)"
- name: Deploy manifests to production
run: |
flux tag artifact $OCI_REPO:$(git tag --points-at HEAD) --tag production
```

### End-to-end testing

Example workflow for running Flux in Kubernetes Kind:
Expand Down
73 changes: 73 additions & 0 deletions cmd/flux/build_artifact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Copyright 2022 The Flux authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"fmt"
"os"

"github.com/spf13/cobra"

oci "github.com/fluxcd/pkg/oci/client"
)

var buildArtifactCmd = &cobra.Command{
Use: "artifact",
Short: "Build artifact",
Long: `The build artifact command creates an tgz file with the manifests from the given directory.`,
stefanprodan marked this conversation as resolved.
Show resolved Hide resolved
Example: ` # Build the given manifests directory into an artifact
flux build artifact --path ./path/to/local/manifests --output ./path/to/artifact.tgz

# List the files bundles in the artifact
stefanprodan marked this conversation as resolved.
Show resolved Hide resolved
tar -ztvf ./path/to/artifact.tgz
`,
RunE: buildArtifactCmdRun,
}

type buildArtifactFlags struct {
output string
path string
}

var buildArtifactArgs buildArtifactFlags

func init() {
buildArtifactCmd.Flags().StringVar(&buildArtifactArgs.path, "path", "", "Path to the directory where the Kubernetes manifests are located.")
buildArtifactCmd.Flags().StringVarP(&buildArtifactArgs.output, "output", "0", "artifact.tgz", "Path to where the artifact tgz file should be written.")
stefanprodan marked this conversation as resolved.
Show resolved Hide resolved
buildCmd.AddCommand(buildArtifactCmd)
}

func buildArtifactCmdRun(cmd *cobra.Command, args []string) error {
if buildArtifactArgs.path == "" {
return fmt.Errorf("invalid path %q", buildArtifactArgs.path)
}

if fs, err := os.Stat(buildArtifactArgs.path); err != nil || !fs.IsDir() {
return fmt.Errorf("invalid path %q", buildArtifactArgs.path)
stefanprodan marked this conversation as resolved.
Show resolved Hide resolved
}

logger.Actionf("building artifact from %s", buildArtifactArgs.path)

ociClient := oci.NewLocalClient()
if err := ociClient.Build(buildArtifactArgs.output, buildArtifactArgs.path); err != nil {
return fmt.Errorf("bulding artifact failed, error: %w", err)
}

logger.Successf("artifact created at %s", buildArtifactArgs.output)

return nil
}
7 changes: 7 additions & 0 deletions cmd/flux/create_kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ var createKsCmd = &cobra.Command{
--prune=true \
--interval=5m

# Create a Kustomization resource that references an OCIRepository
flux create kustomization podinfo \
--source=OCIRepository/podinfo \
--target-namespace=default \
--prune=true \
--interval=5m

# Create a Kustomization resource that references a Bucket
flux create kustomization secrets \
--source=Bucket/secrets \
Expand Down
16 changes: 16 additions & 0 deletions cmd/flux/create_secret_helm_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2022 The Flux authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
Expand Down
121 changes: 121 additions & 0 deletions cmd/flux/create_secret_oci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright 2022 The Flux authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"context"
"fmt"

"github.com/fluxcd/flux2/internal/utils"
"github.com/fluxcd/flux2/pkg/manifestgen/sourcesecret"
"github.com/google/go-containerregistry/pkg/name"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/yaml"
)

var createSecretOCICmd = &cobra.Command{
Use: "oci [name]",
Short: "Create or update a Kubernetes secret for OCI Registry authentication",
Long: `The create secret oci command generates a Kubernetes secret with `,
stefanprodan marked this conversation as resolved.
Show resolved Hide resolved
Example: ` # Create an OCI authentication secret on disk and encrypt it with Mozilla SOPS
flux create secret oci podinfo-auth \
--url=ghcr.io \
--username=username \
--password=password \
--export > repo-auth.yaml

sops --encrypt --encrypted-regex '^(data|stringData)$' \
stefanprodan marked this conversation as resolved.
Show resolved Hide resolved
--in-place repo-auth.yaml
`,
RunE: createSecretOCICmdRun,
}

type secretOCIFlags struct {
url string
password string
username string
}

var secretOCIArgs = secretOCIFlags{}

func init() {
createSecretOCICmd.Flags().StringVar(&secretOCIArgs.url, "url", "", "oci repository address e.g ghcr.io/stefanprodan/charts")
createSecretOCICmd.Flags().StringVarP(&secretOCIArgs.username, "username", "u", "", "basic authentication username")
createSecretOCICmd.Flags().StringVarP(&secretOCIArgs.password, "password", "p", "", "basic authentication password")

createSecretCmd.AddCommand(createSecretOCICmd)
}

func createSecretOCICmdRun(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("name is required")
}

secretName := args[0]

if secretOCIArgs.url == "" {
return fmt.Errorf("--url is required")
}

if secretOCIArgs.username == "" {
return fmt.Errorf("--username is required")
}

if secretOCIArgs.password == "" {
return fmt.Errorf("--password is required")
}

if _, err := name.ParseReference(secretOCIArgs.url); err != nil {
return fmt.Errorf("error parsing url: '%s'", err)
}

opts := sourcesecret.Options{
Name: secretName,
Namespace: *kubeconfigArgs.Namespace,
Registry: secretOCIArgs.url,
Password: secretOCIArgs.password,
Username: secretOCIArgs.username,
}

secret, err := sourcesecret.Generate(opts)
if err != nil {
return err
}

if createArgs.export {
rootCmd.Println(secret.Content)
return nil
}

ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()
kubeClient, err := utils.KubeClient(kubeconfigArgs, kubeclientOptions)
if err != nil {
return err
}
var s corev1.Secret
if err := yaml.Unmarshal([]byte(secret.Content), &s); err != nil {
return err
}
if err := upsertSecret(ctx, kubeClient, s); err != nil {
return err
}

logger.Actionf("oci secret '%s' created in '%s' namespace", secretName, *kubeconfigArgs.Namespace)
return nil
}
Loading