Skip to content

Commit

Permalink
Installation improvments (#38)
Browse files Browse the repository at this point in the history
* removed components reporter, added events-reporter

* bump
  • Loading branch information
roi-codefresh authored Aug 2, 2021
1 parent 0b952d8 commit 0cf6c02
Show file tree
Hide file tree
Showing 11 changed files with 313 additions and 115 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ coverage.txt

# ides
.vscode
__debug_bin
.idea

# Dependency directories (remove the comment below to include it)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.0.42
VERSION=v0.0.43
OUT_DIR=dist
YEAR?=$(shell date +"%Y")

Expand Down
165 changes: 113 additions & 52 deletions cmd/commands/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type (
RuntimeInstallOptions struct {
RuntimeName string
RuntimeToken string
Insecure bool
Version *semver.Version
gsCloneOpts *git.CloneOptions
insCloneOpts *git.CloneOptions
Expand Down Expand Up @@ -151,6 +152,7 @@ func NewRuntimeInstallCommand() *cobra.Command {
return RunRuntimeInstall(ctx, &RuntimeInstallOptions{
RuntimeName: args[0],
Version: version,
Insecure: true,
gsCloneOpts: gsCloneOpts,
insCloneOpts: insCloneOpts,
KubeFactory: f,
Expand Down Expand Up @@ -185,7 +187,7 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {

runtimeCreationResponse, err := cfConfig.NewClient().ArgoRuntime().Create(opts.RuntimeName)
if err != nil {
return fmt.Errorf("failed to get a runtime creation response: %w", err)
return fmt.Errorf("failed to create a new runtime: %w", err)
}

opts.RuntimeToken = runtimeCreationResponse.NewAccessToken
Expand All @@ -196,6 +198,7 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
Namespace: opts.RuntimeName,
KubeFactory: opts.KubeFactory,
CloneOptions: opts.insCloneOpts,
Insecure: opts.Insecure,
})
if err != nil {
return fmt.Errorf("failed to bootstrap repository: %w", err)
Expand Down Expand Up @@ -223,8 +226,12 @@ func RunRuntimeInstall(ctx context.Context, opts *RuntimeInstallOptions) error {
return fmt.Errorf("failed to create codefresh-cm: %w", err)
}

if err = createComponentsReporter(ctx, opts.insCloneOpts, opts); err != nil {
return fmt.Errorf("failed to create components-reporter: %w", err)
if err = createEventsReporter(ctx, opts.insCloneOpts, opts); err != nil {
return fmt.Errorf("failed to create events-reporter: %w", err)
}

if err = createWorkflowReporter(ctx, opts.insCloneOpts, opts); err != nil {
return fmt.Errorf("failed to create workflows-reporter: %w", err)
}

if err = createDemoWorkflowTemplate(ctx, opts.gsCloneOpts, store.Get().GitSourceName, opts.RuntimeName); err != nil {
Expand Down Expand Up @@ -492,19 +499,24 @@ func persistRuntime(ctx context.Context, cloneOpts *git.CloneOptions, rt *runtim
return err
}

func createComponentsReporter(ctx context.Context, cloneOpts *git.CloneOptions, opts *RuntimeInstallOptions) error {
tokenSecret, err := getTokenSecret(opts.RuntimeName, opts.RuntimeToken)
func createEventsReporter(ctx context.Context, cloneOpts *git.CloneOptions, opts *RuntimeInstallOptions) error {
runtimeTokenSecret, err := getRuntimeTokenSecret(opts.RuntimeName, opts.RuntimeToken)
if err != nil {
return fmt.Errorf("failed to create codefresh token secret: %w", err)
}

if err = opts.KubeFactory.Apply(ctx, opts.RuntimeName, tokenSecret); err != nil {
argoTokenSecret, err := getArgoCDTokenSecret(ctx, opts.RuntimeName, opts.Insecure)
if err != nil {
return fmt.Errorf("failed to create argocd token secret: %w", err)
}

if err = opts.KubeFactory.Apply(ctx, opts.RuntimeName, aputil.JoinManifests(runtimeTokenSecret, argoTokenSecret)); err != nil {
return fmt.Errorf("failed to create codefresh token: %w", err)
}

resPath := cloneOpts.FS.Join(apstore.Default.AppsDir, store.Get().ComponentsReporterName, opts.RuntimeName, "resources")
resPath := cloneOpts.FS.Join(apstore.Default.AppsDir, store.Get().EventsReporterName, opts.RuntimeName, "resources")
appDef := &runtime.AppDef{
Name: store.Get().ComponentsReporterName,
Name: store.Get().EventsReporterName,
Type: application.AppTypeDirectory,
URL: cloneOpts.URL() + "/" + resPath,
}
Expand All @@ -521,21 +533,50 @@ func createComponentsReporter(ctx context.Context, cloneOpts *git.CloneOptions,
return err
}

if err := createRBAC(repofs, resPath, opts.RuntimeName); err != nil {
if err := createEventsReporterEventSource(repofs, resPath, opts.RuntimeName, opts.Insecure); err != nil {
return err
}

if err := createEventSource(repofs, resPath, opts.RuntimeName); err != nil {
if err := createSensor(repofs, store.Get().EventsReporterName, resPath, opts.RuntimeName, store.Get().EventsReporterName, "events"); err != nil {
return err
}

if err := createSensor(repofs, store.Get().ComponentsReporterName, resPath, opts.RuntimeName,
store.Get().ComponentsReporterName, opts.commonConfig.CodefreshBaseURL); err != nil {
_, err = r.Persist(ctx, &git.PushOptions{
CommitMsg: "Created Codefresh Event Reporter",
})
return err
}

func createWorkflowReporter(ctx context.Context, cloneOpts *git.CloneOptions, opts *RuntimeInstallOptions) error {
resPath := cloneOpts.FS.Join(apstore.Default.AppsDir, store.Get().WorkflowReporterName, opts.RuntimeName, "resources")
appDef := &runtime.AppDef{
Name: store.Get().WorkflowReporterName,
Type: application.AppTypeDirectory,
URL: cloneOpts.URL() + "/" + resPath,
}
if err := appDef.CreateApp(ctx, opts.KubeFactory, cloneOpts, opts.RuntimeName, store.Get().CFComponentType, opts.Version); err != nil {
return err
}

r, repofs, err := cloneOpts.GetRepo(ctx)
if err != nil {
return err
}

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

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

if err := createSensor(repofs, store.Get().WorkflowReporterName, resPath, opts.RuntimeName, store.Get().WorkflowReporterName, "workflows"); err != nil {
return err
}

_, err = r.Persist(ctx, &git.PushOptions{
CommitMsg: "Created Codefresh Resources",
CommitMsg: "Created Codefresh Workflow Reporter",
})
return err
}
Expand Down Expand Up @@ -565,7 +606,7 @@ var getProjectInfoFromFile = func(repofs fs.FS, name string) (*argocdv1alpha1.Ap
return proj, appSet, nil
}

func getTokenSecret(namespace string, token string) ([]byte, error) {
func getRuntimeTokenSecret(namespace string, token string) ([]byte, error) {
return yaml.Marshal(&v1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Expand All @@ -581,14 +622,35 @@ func getTokenSecret(namespace string, token string) ([]byte, error) {
})
}

func createRBAC(repofs fs.FS, path, runtimeName string) error {
func getArgoCDTokenSecret(ctx context.Context, namespace string, insecure bool) ([]byte, error) {
token, err := cdutil.GenerateToken(ctx, namespace, "admin", nil, insecure)
if err != nil {
return nil, err
}

return yaml.Marshal(&v1.Secret{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Secret",
},
ObjectMeta: metav1.ObjectMeta{
Name: store.Get().ArgoCDTokenSecret,
Namespace: namespace,
},
Data: map[string][]byte{
store.Get().ArgoCDTokenKey: []byte(token),
},
})
}

func createWorkflowReporterRBAC(repofs fs.FS, path, runtimeName string) error {
serviceAccount := &v1.ServiceAccount{
TypeMeta: metav1.TypeMeta{
Kind: "ServiceAccount",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: store.Get().ComponentsReporterSA,
Name: store.Get().CodefreshSA,
Namespace: runtimeName,
},
}
Expand All @@ -599,7 +661,7 @@ func createRBAC(repofs fs.FS, path, runtimeName string) error {
APIVersion: "rbac.authorization.k8s.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: store.Get().ComponentsReporterName,
Name: store.Get().CodefreshSA,
Namespace: runtimeName,
},
Rules: []rbacv1.PolicyRule{
Expand All @@ -617,74 +679,73 @@ func createRBAC(repofs fs.FS, path, runtimeName string) error {
APIVersion: "rbac.authorization.k8s.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: store.Get().ComponentsReporterName,
Name: store.Get().CodefreshSA,
Namespace: runtimeName,
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Namespace: runtimeName,
Name: store.Get().ComponentsReporterSA,
Name: store.Get().CodefreshSA,
},
},
RoleRef: rbacv1.RoleRef{
Kind: "Role",
Name: store.Get().ComponentsReporterName,
Name: store.Get().CodefreshSA,
},
}

return repofs.WriteYamls(repofs.Join(path, "rbac.yaml"), serviceAccount, role, roleBinding)
}

func createEventSource(repofs fs.FS, path, namespace string) error {
func createEventsReporterEventSource(repofs fs.FS, path, namespace string, insecure bool) error {
port := 443
if insecure {
port = 80
}
argoCDSvc := fmt.Sprintf("argocd-server.%s.svc:%d", namespace, port)

eventSource := eventsutil.CreateEventSource(&eventsutil.CreateEventSourceOptions{
Name: store.Get().EventsReporterName,
Namespace: namespace,
EventBusName: store.Get().EventBusName,
Generic: map[string]eventsutil.CreateGenericEventSourceOptions{
"events": {
URL: argoCDSvc,
TokenSecretName: store.Get().ArgoCDTokenSecret,
Insecure: insecure,
},
},
})
return repofs.WriteYamls(repofs.Join(path, "event-source.yaml"), eventSource)
}

func createWorkflowReporterEventSource(repofs fs.FS, path, namespace string) error {
eventSource := eventsutil.CreateEventSource(&eventsutil.CreateEventSourceOptions{
Name: store.Get().ComponentsReporterName,
Name: store.Get().WorkflowReporterName,
Namespace: namespace,
ServiceAccountName: store.Get().ComponentsReporterSA,
ServiceAccountName: store.Get().CodefreshSA,
EventBusName: store.Get().EventBusName,
Resource: map[string]eventsutil.CreateResourceEventSourceOptions{
"components": {
Group: "argoproj.io",
Version: "v1alpha1",
Resource: "applications",
Namespace: namespace,
Selectors: []eventsutil.CreateSelectorOptions{
{
Key: store.Get().LabelKeyCFType,
Operation: "==",
Value: store.Get().CFComponentType,
},
},
},
"runtime": {
"workflows": {
Group: "argoproj.io",
Version: "v1alpha1",
Resource: "appprojects",
Resource: "workflows",
Namespace: namespace,
Selectors: []eventsutil.CreateSelectorOptions{
{
Key: store.Get().LabelKeyCFType,
Operation: "==",
Value: store.Get().CFRuntimeType,
},
},
},
},
})
return repofs.WriteYamls(repofs.Join(path, "event-source.yaml"), eventSource)
}

func createSensor(repofs fs.FS, name, path, namespace, eventSourceName, cfBaseURL string) error {
func createSensor(repofs fs.FS, name, path, namespace, eventSourceName, trigger string) error {
sensor := eventsutil.CreateSensor(&eventsutil.CreateSensorOptions{
Name: name,
Namespace: namespace,
EventSourceName: eventSourceName,
EventBusName: store.Get().EventBusName,
TriggerURL: cfBaseURL + store.Get().EventReportingEndpoint,
Triggers: []string{
"components",
"runtime",
},
TriggerURL: cfConfig.GetCurrentContext().URL + store.Get().EventReportingEndpoint,
Triggers: []string{trigger},
})
return repofs.WriteYamls(repofs.Join(path, "sensor.yaml"), sensor)
}
Expand Down Expand Up @@ -752,7 +813,7 @@ func createGitSource(ctx context.Context, insCloneOpts *git.CloneOptions, gsClon
eventSource := eventsutil.CreateEventSource(&eventsutil.CreateEventSourceOptions{
Name: eventSourceName,
Namespace: runtimeName,
ServiceAccountName: store.Get().ComponentsReporterSA,
ServiceAccountName: store.Get().CodefreshSA,
EventBusName: store.Get().EventBusName,
Resource: map[string]eventsutil.CreateResourceEventSourceOptions{
// "clusterWorkflowTemplate": {
Expand Down
6 changes: 3 additions & 3 deletions docs/releases/release_notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
### Installed Applications:
* Argo CD [v2.0.5](https://github.com/argoproj/argo-cd/releases/tag/v2.0.5)
* Argo CD [v2.1.0-rc1](https://github.com/codefresh-io/argo-cd/releases/tag/v2.1.0-rc1)
* 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.2](https://github.com/argoproj/argo-rollouts/releases/tag/v1.0.2)
Expand All @@ -8,7 +8,7 @@
### Linux
```bash
# download and extract the binary
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.42/cf-linux-amd64.tar.gz | tar zx
curl -L --output - https://github.com/codefresh-io/cli-v2/releases/download/v0.0.43/cf-linux-amd64.tar.gz | tar zx

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

# move the binary to your $PATH
mv ./cf-darwin-amd64 /usr/local/bin/cf
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ go 1.16
require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/argoproj-labs/applicationset v0.1.0
github.com/argoproj-labs/argocd-autopilot v0.2.12
github.com/argoproj/argo-cd/v2 v2.0.3
github.com/argoproj-labs/argocd-autopilot v0.2.13
github.com/argoproj/argo-cd/v2 v2.1.0-rc1
github.com/argoproj/argo-events v1.3.1
github.com/argoproj/argo-workflows/v3 v3.1.0
github.com/argoproj/gitops-engine v0.3.3 // indirect
github.com/briandowns/spinner v1.13.0
github.com/codefresh-io/go-sdk v0.30.1
github.com/fatih/color v1.12.0
Expand All @@ -30,6 +31,7 @@ require (
replace (
github.com/argoproj-labs/applicationset => github.com/argoproj-labs/applicationset v0.0.0-20210614145856-2c62537a8e5a
github.com/argoproj/argo-events => github.com/argoproj/argo-events v0.17.1-0.20210615165534-d403c441bc1d
github.com/argoproj/gitops-engine => github.com/argoproj/gitops-engine v0.3.1-0.20210709004906-a4c77d5c70fb
k8s.io/api => k8s.io/api v0.21.1
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.21.1
k8s.io/apimachinery => k8s.io/apimachinery v0.21.1
Expand Down
Loading

0 comments on commit 0cf6c02

Please sign in to comment.