Skip to content

Commit

Permalink
add addon example with cloudevents.
Browse files Browse the repository at this point in the history
Signed-off-by: morvencao <[email protected]>
  • Loading branch information
morvencao committed Apr 11, 2024
1 parent 0007165 commit ed7b798
Show file tree
Hide file tree
Showing 38 changed files with 1,667 additions and 67 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/go-presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,38 @@ jobs:
env:
KUBECONFIG: /home/runner/.kube/config

e2e-cloudevents:
name: e2e-cloudevents
runs-on: ubuntu-latest
steps:
- name: checkout code
uses: actions/checkout@v3
with:
fetch-depth: 1
path: go/src/open-cluster-management.io/addon-framework
- name: install Go
uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}
- name: install imagebuilder
run: go install github.com/openshift/imagebuilder/cmd/[email protected]
- name: addon-examples-image # will build and tag the examples image
run: imagebuilder --allow-pull -t quay.io/open-cluster-management/addon-examples:latest -t quay.io/ocm/addon-examples:latest -f ./build/Dockerfile.example .
- name: setup kind
uses: engineerd/[email protected]
with:
version: v0.11.1
name: cluster1
- name: Load image on the nodes of the cluster
run: |
kind load docker-image --name=cluster1 quay.io/open-cluster-management/addon-examples:latest
kind load docker-image --name=cluster1 quay.io/ocm/addon-examples:latest
- name: Run e2e test with cloudevents
run: |
make test-e2e-cloudevents
env:
KUBECONFIG: /home/runner/.kube/config

e2e-hosted:
name: e2e-hosted
runs-on: ubuntu-latest
Expand Down
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ verify: verify-gocilint
deploy-ocm:
examples/deploy/ocm/install.sh

deploy-ocm-cloudevents:
examples/deploy/ocm-cloudevents/install.sh

deploy-hosted-ocm:
examples/deploy/hosted-ocm/install.sh

Expand All @@ -71,6 +74,12 @@ deploy-helloworld: ensure-kustomize
$(KUSTOMIZE) build examples/deploy/addon/helloworld | $(KUBECTL) apply -f -
mv examples/deploy/addon/helloworld/kustomization.yaml.tmp examples/deploy/addon/helloworld/kustomization.yaml

deploy-helloworld-cloudevents: ensure-kustomize
cp examples/deploy/addon/helloworld-cloudevents/kustomization.yaml examples/deploy/addon/helloworld-cloudevents/kustomization.yaml.tmp
cd examples/deploy/addon/helloworld-cloudevents && ../../../../$(KUSTOMIZE) edit set image quay.io/open-cluster-management/addon-examples=$(EXAMPLE_IMAGE_NAME)
$(KUSTOMIZE) build examples/deploy/addon/helloworld-cloudevents | $(KUBECTL) apply -f -
mv examples/deploy/addon/helloworld-cloudevents/kustomization.yaml.tmp examples/deploy/addon/helloworld-cloudevents/kustomization.yaml

deploy-helloworld-helm: ensure-kustomize
cp examples/deploy/addon/helloworld-helm/kustomization.yaml examples/deploy/addon/helloworld-helm/kustomization.yaml.tmp
cd examples/deploy/addon/helloworld-helm && ../../../../$(KUSTOMIZE) edit set image quay.io/open-cluster-management/addon-examples=$(EXAMPLE_IMAGE_NAME)
Expand Down Expand Up @@ -111,6 +120,9 @@ undeploy-busybox: ensure-kustomize
undeploy-helloworld: ensure-kustomize
$(KUSTOMIZE) build examples/deploy/addon/helloworld | $(KUBECTL) delete --ignore-not-found -f -

undeploy-helloworld-cloudevents: ensure-kustomize
$(KUSTOMIZE) build examples/deploy/addon/helloworld-cloudevents | $(KUBECTL) delete --ignore-not-found -f -

undeploy-helloworld-helm: ensure-kustomize
$(KUSTOMIZE) build examples/deploy/addon/helloworld-helm | $(KUBECTL) delete --ignore-not-found -f -

Expand All @@ -129,6 +141,12 @@ build-e2e:
test-e2e: build-e2e deploy-ocm deploy-helloworld deploy-helloworld-helm
./e2e.test -test.v -ginkgo.v

build-e2e-cloudevents:
go test -c ./test/e2ecloudevents

test-e2e-cloudevents: build-e2e-cloudevents deploy-ocm-cloudevents deploy-helloworld-cloudevents
./e2ecloudevents.test -test.v -ginkgo.v

build-hosted-e2e:
go test -c ./test/e2ehosted

Expand Down
1 change: 1 addition & 0 deletions build/Dockerfile.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ RUN make build --warn-undefined-variables
FROM registry.access.redhat.com/ubi8/ubi-minimal:latest
COPY --from=builder /go/src/open-cluster-management.io/addon-framework/busybox /
COPY --from=builder /go/src/open-cluster-management.io/addon-framework/helloworld /
COPY --from=builder /go/src/open-cluster-management.io/addon-framework/helloworld_cloudevents /
COPY --from=builder /go/src/open-cluster-management.io/addon-framework/helloworld_helm /
COPY --from=builder /go/src/open-cluster-management.io/addon-framework/helloworld_hosted /

2 changes: 1 addition & 1 deletion cmd/example/busybox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
if err != nil {
os.Exit(1)
}
addonMgr, err := addonmanager.New(kubeConfig, addonmanager.NewManagerOptions())
addonMgr, err := addonmanager.New(kubeConfig)
if err != nil {
klog.Errorf("unable to setup addon manager: %v", err)
os.Exit(1)
Expand Down
14 changes: 3 additions & 11 deletions cmd/example/helloworld/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,22 @@ func newCommand() *cobra.Command {
}

func newControllerCommand() *cobra.Command {
o := addonmanager.NewManagerOptions()
c := &addManagerConfig{managerOptions: o}
cmd := cmdfactory.
NewControllerCommandConfig("helloworld-addon-controller", version.Get(), c.runController).
NewControllerCommandConfig("helloworld-addon-controller", version.Get(), runController).
NewCommand()
cmd.Use = "controller"
cmd.Short = "Start the addon controller"
o.AddFlags(cmd)

return cmd
}

// addManagerConfig holds configuration for addon manager
type addManagerConfig struct {
managerOptions *addonmanager.ManagerOptions
}

func (c *addManagerConfig) runController(ctx context.Context, kubeConfig *rest.Config) error {
func runController(ctx context.Context, kubeConfig *rest.Config) error {
addonClient, err := addonv1alpha1client.NewForConfig(kubeConfig)
if err != nil {
return err
}

mgr, err := addonmanager.New(kubeConfig, c.managerOptions)
mgr, err := addonmanager.New(kubeConfig)
if err != nil {
return err
}
Expand Down
145 changes: 145 additions & 0 deletions cmd/example/helloworld_cloudevents/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package main

import (
"context"
goflag "flag"
"fmt"
"math/rand"
"os"
"time"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
utilrand "k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/rest"
utilflag "k8s.io/component-base/cli/flag"
logs "k8s.io/component-base/logs"
"k8s.io/klog/v2"
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned"

"open-cluster-management.io/addon-framework/examples/helloworld_agent"
"open-cluster-management.io/addon-framework/examples/helloworld_cloudevents"
"open-cluster-management.io/addon-framework/pkg/addonfactory"
"open-cluster-management.io/addon-framework/pkg/addonmanager"
cmdfactory "open-cluster-management.io/addon-framework/pkg/cmd/factory"
"open-cluster-management.io/addon-framework/pkg/utils"
"open-cluster-management.io/addon-framework/pkg/version"
)

func main() {
rand.Seed(time.Now().UTC().UnixNano())

pflag.CommandLine.SetNormalizeFunc(utilflag.WordSepNormalizeFunc)
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)

logs.AddFlags(pflag.CommandLine)
logs.InitLogs()
defer logs.FlushLogs()

command := newCommand()
if err := command.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}

func newCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "addon",
Short: "helloworld example addon",
Run: func(cmd *cobra.Command, args []string) {
if err := cmd.Help(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
os.Exit(1)
},
}

if v := version.Get().String(); len(v) == 0 {
cmd.Version = "<unknown>"
} else {
cmd.Version = v
}

cmd.AddCommand(newControllerCommand())
cmd.AddCommand(helloworld_agent.NewAgentCommand(helloworld_cloudevents.AddonName))

return cmd
}

func newControllerCommand() *cobra.Command {
o := addonmanager.NewManagerOptions()
c := &addManagerConfig{managerOptions: o}
cmd := cmdfactory.
NewControllerCommandConfig("helloworld-addon-controller", version.Get(), c.runController).
NewCommand()
cmd.Use = "controller"
cmd.Short = "Start the addon controller"
o.AddFlags(cmd)

return cmd
}

// addManagerConfig holds configuration for addon manager
type addManagerConfig struct {
managerOptions *addonmanager.ManagerOptions
}

func (c *addManagerConfig) runController(ctx context.Context, kubeConfig *rest.Config) error {
addonClient, err := addonv1alpha1client.NewForConfig(kubeConfig)
if err != nil {
return err
}

mgr, err := addonmanager.New(kubeConfig, c.managerOptions)
if err != nil {
return err
}

registrationOption := helloworld_cloudevents.NewRegistrationOption(
kubeConfig,
helloworld_cloudevents.AddonName,
utilrand.String(5),
)

// Set agent install namespace from addon deployment config if it exists
registrationOption.AgentInstallNamespace = utils.AgentInstallNamespaceFromDeploymentConfigFunc(
utils.NewAddOnDeploymentConfigGetter(addonClient),
)

agentAddon, err := addonfactory.NewAgentAddonFactory(helloworld_cloudevents.AddonName, helloworld_cloudevents.FS, "manifests/templates").
WithConfigGVRs(utils.AddOnDeploymentConfigGVR).
WithGetValuesFuncs(
helloworld_cloudevents.GetDefaultValues,
addonfactory.GetAddOnDeploymentConfigValues(
utils.NewAddOnDeploymentConfigGetter(addonClient),
addonfactory.ToAddOnDeploymentConfigValues,
addonfactory.ToImageOverrideValuesFunc("Image", helloworld_cloudevents.DefaultImage),
),
).
WithAgentRegistrationOption(registrationOption).
WithAgentInstallNamespace(
utils.AgentInstallNamespaceFromDeploymentConfigFunc(
utils.NewAddOnDeploymentConfigGetter(addonClient),
),
).
WithAgentHealthProber(helloworld_cloudevents.AgentHealthProber()).
BuildTemplateAgentAddon()
if err != nil {
klog.Errorf("failed to build agent %v", err)
return err
}

err = mgr.AddAgent(agentAddon)
if err != nil {
klog.Fatal(err)
}

err = mgr.Start(ctx)
if err != nil {
klog.Fatal(err)
}
<-ctx.Done()

return nil
}
14 changes: 3 additions & 11 deletions cmd/example/helloworld_helm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,16 @@ func newCommand() *cobra.Command {
}

func newControllerCommand() *cobra.Command {
o := addonmanager.NewManagerOptions()
c := &addManagerConfig{managerOptions: o}
cmd := cmdfactory.
NewControllerCommandConfig("helloworldhelm-addon-controller", version.Get(), c.runController).
NewControllerCommandConfig("helloworldhelm-addon-controller", version.Get(), runController).
NewCommand()
cmd.Use = "controller"
cmd.Short = "Start the addon controller"
o.AddFlags(cmd)

return cmd
}

// addManagerConfig holds configuration for addon manager
type addManagerConfig struct {
managerOptions *addonmanager.ManagerOptions
}

func (c *addManagerConfig) runController(ctx context.Context, kubeConfig *rest.Config) error {
func runController(ctx context.Context, kubeConfig *rest.Config) error {
kubeClient, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
return err
Expand All @@ -97,7 +89,7 @@ func (c *addManagerConfig) runController(ctx context.Context, kubeConfig *rest.C
return err
}

mgr, err := addonmanager.New(kubeConfig, c.managerOptions)
mgr, err := addonmanager.New(kubeConfig)
if err != nil {
klog.Errorf("failed to new addon manager %v", err)
return err
Expand Down
14 changes: 3 additions & 11 deletions cmd/example/helloworld_hosted/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,17 @@ func newCommand() *cobra.Command {
}

func newControllerCommand() *cobra.Command {
o := addonmanager.NewManagerOptions()
c := &addManagerConfig{managerOptions: o}
cmd := cmdfactory.
NewControllerCommandConfig("helloworld-addon-controller", version.Get(), c.runController).
NewControllerCommandConfig("helloworld-addon-controller", version.Get(), runController).
NewCommand()
cmd.Use = "controller"
cmd.Short = "Start the addon controller"
o.AddFlags(cmd)

return cmd
}

// addManagerConfig holds configuration for addon manager
type addManagerConfig struct {
managerOptions *addonmanager.ManagerOptions
}

func (c *addManagerConfig) runController(ctx context.Context, kubeConfig *rest.Config) error {
mgr, err := addonmanager.New(kubeConfig, c.managerOptions)
func runController(ctx context.Context, kubeConfig *rest.Config) error {
mgr, err := addonmanager.New(kubeConfig)
if err != nil {
return err
}
Expand Down
28 changes: 28 additions & 0 deletions examples/deploy/addon/helloworld-cloudevents/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace: open-cluster-management

resources:
- resources/cluster_role.yaml
- resources/cluster_role_binding.yaml
- resources/service_account.yaml
- resources/managed_clusterset_binding.yaml
- resources/placement.yaml
- resources/addon_deployment_config.yaml
- resources/helloworld_cloudevents_clustermanagementaddon.yaml
- resources/helloworld_cloudevents_controller.yaml
- resources/work-driver-config.yaml

images:
- name: quay.io/open-cluster-management/addon-examples
newName: quay.io/open-cluster-management/addon-examples
newTag: latest
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

vars:
- name: EXAMPLE_IMAGE_NAME
objref:
apiVersion: apps/v1
kind: Deployment
name: helloworldcloudevents-controller
fieldref:
fieldpath: spec.template.spec.containers.[name=helloworldcloudevents-controller].image
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: addon.open-cluster-management.io/v1alpha1
kind: AddOnDeploymentConfig
metadata:
name: global
spec:
agentInstallNamespace: open-cluster-management-agent-addon
Loading

0 comments on commit ed7b798

Please sign in to comment.