Skip to content

Commit

Permalink
CR-6135 - workflows ingress (#75)
Browse files Browse the repository at this point in the history
* add resources and patches for workflows ingress
  • Loading branch information
ATGardner authored Aug 26, 2021
1 parent 5dddc4a commit 408d6b2
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.0.78
VERSION=v0.0.79
OUT_DIR=dist
YEAR?=$(shell date +"%Y")

Expand Down
12 changes: 12 additions & 0 deletions cmd/commands/assets/ingress-patch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"op": "add",
"path": "/spec/template/spec/containers/0/env",
"value": [
{
"name": "BASE_HREF",
"value": "/workflows/"
}
]
}
]
4 changes: 4 additions & 0 deletions cmd/commands/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package commands

import (
_ "embed"
"os"
"regexp"

Expand All @@ -30,6 +31,9 @@ var (
die = util.Die
exit = os.Exit

//go:embed assets/ingress-patch.json
ingressPatch []byte

cfConfig *config.Config
)

Expand Down
6 changes: 3 additions & 3 deletions cmd/commands/git-source.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func RunGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) error
}

if len(fi) == 0 {
if err = createDemoWorkflowTemplate(gsFs, opts.gsName, opts.runtimeName); err != nil {
if err = createDemoWorkflowTemplate(gsFs, opts.runtimeName); err != nil {
return fmt.Errorf("failed to create demo workflowTemplate: %w", err)
}

Expand All @@ -183,7 +183,7 @@ func RunGitSourceCreate(ctx context.Context, opts *GitSourceCreateOptions) error
Type: application.AppTypeDirectory,
URL: opts.gsCloneOpts.Repo,
}
if err := appDef.CreateApp(ctx, nil, opts.insCloneOpts, opts.runtimeName, store.Get().CFGitSourceType, nil); err != nil {
if err := appDef.CreateApp(ctx, nil, opts.insCloneOpts, opts.runtimeName, store.Get().CFGitSourceType); err != nil {
return fmt.Errorf("failed to create git-source application. Err: %w", err)
}

Expand Down Expand Up @@ -402,7 +402,7 @@ func RunGitSourceEdit(ctx context.Context, opts *GitSourceEditOptions) error {
return nil
}

func createDemoWorkflowTemplate(gsFs fs.FS, gsName, runtimeName string) error {
func createDemoWorkflowTemplate(gsFs fs.FS, runtimeName string) error {
wfTemplate := &wfv1alpha1.WorkflowTemplate{
TypeMeta: metav1.TypeMeta{
Kind: wf.WorkflowTemplateKind,
Expand Down
105 changes: 89 additions & 16 deletions cmd/commands/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import (
"github.com/codefresh-io/cli-v2/pkg/util"
cdutil "github.com/codefresh-io/cli-v2/pkg/util/cd"
eventsutil "github.com/codefresh-io/cli-v2/pkg/util/events"
ingressutil "github.com/codefresh-io/cli-v2/pkg/util/ingress"
kustutil "github.com/codefresh-io/cli-v2/pkg/util/kust"
"github.com/codefresh-io/go-sdk/pkg/codefresh/model"

"github.com/Masterminds/semver/v3"
appset "github.com/argoproj-labs/applicationset/api/v1alpha1"
apcmd "github.com/argoproj-labs/argocd-autopilot/cmd/commands"
"github.com/argoproj-labs/argocd-autopilot/pkg/application"
Expand All @@ -39,20 +40,28 @@ import (
apstore "github.com/argoproj-labs/argocd-autopilot/pkg/store"
aputil "github.com/argoproj-labs/argocd-autopilot/pkg/util"
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
argowf "github.com/argoproj/argo-workflows/v3/pkg/apis/workflow"

"github.com/Masterminds/semver/v3"
"github.com/ghodss/yaml"
"github.com/go-git/go-billy/v5/memfs"
billyUtils "github.com/go-git/go-billy/v5/util"
"github.com/juju/ansiterm"
"github.com/spf13/cobra"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kustid "sigs.k8s.io/kustomize/api/resid"
kusttypes "sigs.k8s.io/kustomize/api/types"
)

type (
RuntimeInstallOptions struct {
RuntimeName string
RuntimeToken string
IngressHost string
Insecure bool
Version *semver.Version
gsCloneOpts *git.CloneOptions
Expand Down Expand Up @@ -97,6 +106,7 @@ func NewRuntimeCommand() *cobra.Command {

func NewRuntimeInstallCommand() *cobra.Command {
var (
ingressHost string
versionStr string
f kube.Factory
insCloneOpts *git.CloneOptions
Expand Down Expand Up @@ -161,6 +171,7 @@ func NewRuntimeInstallCommand() *cobra.Command {

return RunRuntimeInstall(ctx, &RuntimeInstallOptions{
RuntimeName: args[0],
IngressHost: ingressHost,
Version: version,
Insecure: true,
gsCloneOpts: gsCloneOpts,
Expand All @@ -173,6 +184,7 @@ func NewRuntimeInstallCommand() *cobra.Command {
},
}

cmd.Flags().StringVar(&ingressHost, "ingress-host", "", "The ingress host")
cmd.Flags().StringVar(&versionStr, "version", "", "The runtime version to install, defaults to latest")
cmd.Flags().DurationVar(&store.Get().WaitTimeout, "wait-timeout", store.Get().WaitTimeout, "How long to wait for the runtime components to be ready")

Expand Down Expand Up @@ -207,12 +219,13 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
}

opts.RuntimeToken = runtimeCreationResponse.NewAccessToken

server, err := util.CurrentServer()
if err != nil {
return fmt.Errorf("failed to get current server address: %w", err)
}

rt.Spec.Cluster = server
rt.Spec.IngressHost = opts.IngressHost

log.G(ctx).WithField("version", rt.Spec.Version).Infof("installing runtime '%s'", opts.RuntimeName)
err = apcmd.RunRepoBootstrap(ctx, &apcmd.RepoBootstrapOptions{
Expand Down Expand Up @@ -248,11 +261,17 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {

for _, component := range rt.Spec.Components {
log.G(ctx).Infof("creating component '%s'", component.Name)
if err = component.CreateApp(ctx, opts.KubeFactory, opts.insCloneOpts, opts.RuntimeName, store.Get().CFComponentType, rt.Spec.Version); err != nil {
if err = component.CreateApp(ctx, opts.KubeFactory, opts.insCloneOpts, opts.RuntimeName, store.Get().CFComponentType); err != nil {
return fmt.Errorf("failed to create '%s' application: %w", component.Name, err)
}
}

if opts.IngressHost != "" {
if err = createWorkflowsIngress(ctx, opts.insCloneOpts, rt); err != nil {
return fmt.Errorf("failed to patch Argo-Workflows ingress: %w", err)
}
}

if err = createEventsReporter(ctx, opts.insCloneOpts, opts, rt); err != nil {
return fmt.Errorf("failed to create events-reporter: %w", err)
}
Expand Down Expand Up @@ -615,7 +634,7 @@ func RunRuntimeUpgrade(ctx context.Context, opts *RuntimeUpgradeOptions) error {

for _, component := range newComponents {
log.G(ctx).Infof("Creating app '%s'", component.Name)
if err = component.CreateApp(ctx, nil, opts.CloneOpts, opts.RuntimeName, store.Get().CFComponentType, newRt.Spec.Version); err != nil {
if err = component.CreateApp(ctx, nil, opts.CloneOpts, opts.RuntimeName, store.Get().CFComponentType); err != nil {
return fmt.Errorf("failed to create '%s' application: %w", component.Name, err)
}
}
Expand All @@ -629,7 +648,11 @@ func persistRuntime(ctx context.Context, cloneOpts *git.CloneOptions, rt *runtim
return err
}

if err = rt.Save(fs, fs.Join(apstore.Default.BootsrtrapDir, store.Get().RuntimeFilename), rtConf); err != nil {
if err = rt.Save(fs, fs.Join(apstore.Default.BootsrtrapDir, rt.Name+".yaml"), rtConf); err != nil {
return err
}

if err := updateProject(fs, rt); err != nil {
return err
}

Expand All @@ -639,6 +662,57 @@ func persistRuntime(ctx context.Context, cloneOpts *git.CloneOptions, rt *runtim
return err
}

func createWorkflowsIngress(ctx context.Context, cloneOpts *git.CloneOptions, rt *runtime.Runtime) error {
r, fs, err := cloneOpts.GetRepo(ctx)
if err != nil {
return err
}

overlaysDir := fs.Join(apstore.Default.AppsDir, "workflows", apstore.Default.OverlaysDir, rt.Name)
ingress := ingressutil.CreateIngress(&ingressutil.CreateIngressOptions{
Name: rt.Name + store.Get().IngressName,
Namespace: rt.Namespace,
Path: store.Get().IngressPath,
ServiceName: store.Get().ArgoWFServiceName,
ServicePort: store.Get().ArgoWFServicePort,
})
if err = fs.WriteYamls(fs.Join(overlaysDir, "ingress.yaml"), ingress); err != nil {
return err
}

if err = billyUtils.WriteFile(fs, fs.Join(overlaysDir, "ingress-patch.json"), ingressPatch, 0666); err != nil {
return err
}

kust, err := kustutil.ReadKustomization(fs, overlaysDir)
if err != nil {
return err
}

kust.Resources = append(kust.Resources, "ingress.yaml")
kust.Patches = append(kust.Patches, kusttypes.Patch{
Target: &kusttypes.Selector{
KrmId: kusttypes.KrmId{
Gvk: kustid.Gvk{
Group: appsv1.SchemeGroupVersion.Group,
Version: appsv1.SchemeGroupVersion.Version,
Kind: "Deployment",
},
Name: store.Get().ArgoWFServiceName,
},
},
Path: "ingress-patch.json",
})
if err = kustutil.WriteKustomization(fs, kust, overlaysDir); err != nil {
return err
}

_, err = r.Persist(ctx, &git.PushOptions{
CommitMsg: "Created Workflows Ingress",
})
return err
}

func createEventsReporter(ctx context.Context, cloneOpts *git.CloneOptions, opts *RuntimeInstallOptions, rt *runtime.Runtime) error {
runtimeTokenSecret, err := getRuntimeTokenSecret(opts.RuntimeName, opts.RuntimeToken)
if err != nil {
Expand All @@ -660,7 +734,7 @@ func createEventsReporter(ctx context.Context, cloneOpts *git.CloneOptions, opts
Type: application.AppTypeDirectory,
URL: cloneOpts.URL() + "/" + resPath,
}
if err := appDef.CreateApp(ctx, opts.KubeFactory, cloneOpts, opts.RuntimeName, store.Get().CFComponentType, nil); err != nil {
if err := appDef.CreateApp(ctx, opts.KubeFactory, cloneOpts, opts.RuntimeName, store.Get().CFComponentType); err != nil {
return err
}

Expand All @@ -669,10 +743,6 @@ func createEventsReporter(ctx context.Context, cloneOpts *git.CloneOptions, opts
return err
}

if err := updateProject(repofs, opts.RuntimeName, rt); err != nil {
return err
}

if err := createEventsReporterEventSource(repofs, resPath, opts.RuntimeName, opts.Insecure); err != nil {
return err
}
Expand All @@ -694,7 +764,7 @@ func createWorkflowReporter(ctx context.Context, cloneOpts *git.CloneOptions, op
Type: application.AppTypeDirectory,
URL: cloneOpts.URL() + "/" + resPath,
}
if err := appDef.CreateApp(ctx, opts.KubeFactory, cloneOpts, opts.RuntimeName, store.Get().CFComponentType, opts.Version); err != nil {
if err := appDef.CreateApp(ctx, opts.KubeFactory, cloneOpts, opts.RuntimeName, store.Get().CFComponentType); err != nil {
return err
}

Expand All @@ -721,8 +791,8 @@ func createWorkflowReporter(ctx context.Context, cloneOpts *git.CloneOptions, op
return err
}

func updateProject(repofs fs.FS, runtimeName string, rt *runtime.Runtime) error {
projPath := repofs.Join(apstore.Default.ProjectsDir, runtimeName+".yaml")
func updateProject(repofs fs.FS, rt *runtime.Runtime) error {
projPath := repofs.Join(apstore.Default.ProjectsDir, rt.Name+".yaml")
project, appset, err := getProjectInfoFromFile(repofs, projPath)
if err != nil {
return err
Expand All @@ -739,6 +809,9 @@ func updateProject(repofs fs.FS, runtimeName string, rt *runtime.Runtime) error

project.ObjectMeta.Labels[store.Get().LabelKeyCFType] = store.Get().CFRuntimeType
project.ObjectMeta.Labels[store.Get().LabelKeyRuntimeVersion] = runtimeVersion
if rt.Spec.IngressHost != "" {
project.ObjectMeta.Labels[store.Get().LabelKeyIngressHost] = rt.Spec.IngressHost
}

return repofs.WriteYamls(projPath, project, appset)
}
Expand Down Expand Up @@ -875,9 +948,9 @@ func createWorkflowReporterEventSource(repofs fs.FS, path, namespace string) err
EventBusName: store.Get().EventBusName,
Resource: map[string]eventsutil.CreateResourceEventSourceOptions{
"workflows": {
Group: "argoproj.io",
Version: "v1alpha1",
Resource: "workflows",
Group: argowf.Group,
Version: argowf.Version,
Resource: argowf.WorkflowPlural,
Namespace: namespace,
},
},
Expand Down
1 change: 1 addition & 0 deletions docs/commands/cli-v2_runtime_install.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cli-v2 runtime install [runtime_name] [flags]
--git-src-repo string Repository URL [GIT_SRC_GIT_REPO]
-t, --git-token string Your git provider api token [GIT_TOKEN]
-h, --help help for install
--ingress-host string The ingress host
--kubeconfig string Path to the kubeconfig file to use for CLI requests.
-n, --namespace string If present, the namespace scope for this CLI request
--provider string The git provider, one of: gitea|github
Expand Down
8 changes: 4 additions & 4 deletions docs/releases/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Argo CD [v2.1.0-rc2](https://github.com/codefresh-io/argo-cd/releases/tag/v2.1.0-rc2)
* Argo CD ApplicationSet Controller [2c62537a8e5a](https://github.com/argoproj-labs/applicationset/commit/2c62537a8e5a3d5aecad87b843870789b74bdf89)
* Argo Events [v1.4.0](https://github.com/argoproj/argo-events/releases/tag/v1.4.0)
* Argo Rollouts [v1.0.4](https://github.com/argoproj/argo-rollouts/releases/tag/v1.0.4)
* Argo Workflows [v3.1.6](https://github.com/argoproj/argo-workflows/releases/tag/v3.1.6)
* Argo Rollouts [v1.0.5](https://github.com/argoproj/argo-rollouts/releases/tag/v1.0.5)
* Argo Workflows [v3.1.8](https://github.com/argoproj/argo-workflows/releases/tag/v3.1.8)

### Using brew:
```bash
Expand All @@ -20,7 +20,7 @@ cf version
### Linux
```bash
# download and extract the binary
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.78/cf-linux-amd64.tar.gz | tar zx
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.79/cf-linux-amd64.tar.gz | tar zx

# move the binary to your $PATH
mv ./cf-linux-amd64 /usr/local/bin/cf
Expand All @@ -32,7 +32,7 @@ cf version
### Mac
```bash
# download and extract the binary
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.78/cf-darwin-amd64.tar.gz | tar zx
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.79/cf-darwin-amd64.tar.gz | tar zx

# move the binary to your $PATH
mv ./cf-darwin-amd64 /usr/local/bin/cf
Expand Down
2 changes: 1 addition & 1 deletion manifests/argo-rollouts/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- https://github.com/argoproj/argo-rollouts/releases/download/v1.0.4/install.yaml
- https://github.com/argoproj/argo-rollouts/releases/download/v1.0.5/install.yaml
patches:
- target:
group: apiextensions.k8s.io
Expand Down
12 changes: 11 additions & 1 deletion manifests/argo-workflows/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- https://github.com/argoproj/argo-workflows/releases/download/v3.1.6/install.yaml
- https://github.com/argoproj/argo-workflows/releases/download/v3.1.8/install.yaml

patches:
# reset the crbs to `subject.namespace: default`, so that argo-workflows will later change them to the actual ns
Expand All @@ -13,3 +13,13 @@ patches:
- op: replace
path: /subjects/0/namespace
value: default
- target:
group: apps
version: v1
kind: Deployment
name: argo-server
patch: |-
- op: add
path: /spec/template/spec/containers/0/args/-
value: --auth-mode=server
2 changes: 1 addition & 1 deletion manifests/runtime.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ metadata:
namespace: "{{ namespace }}"
spec:
defVersion: 1.0.0
version: 0.0.75
version: 0.0.79
bootstrapSpecifier: github.com/codefresh-io/cli-v2/manifests/argo-cd
components:
- name: events
Expand Down
Loading

0 comments on commit 408d6b2

Please sign in to comment.