Skip to content

Commit

Permalink
feat: improve workflow (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaLanziani authored Aug 7, 2023
1 parent 2238866 commit 315926b
Show file tree
Hide file tree
Showing 23 changed files with 298 additions and 96 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ You will be able to run the executable from
These are the environment variables that you have to set in order to use the onmain, onbranch commands from your local environment

```
export KKA_REGISTRY_PASSWORD="<lucalprojects_pat>"
export KKA_REGISTRY_USER="lucalprojects"
export KKA_REPO_NAME="ghcr.io/llprojects/registry"
export KKA_REGISTRY_PASSWORD="<github_pat>"
export KKA_REGISTRY_USER="<github_user>"
```

and

```
export KKA_CLUSTER_ENDPOINT=$(kubectl config view -o jsonpath='{.clusters[?(@.name == "kind-k8s-kurated-addons")].cluster.server}')
export KKA_CLUSTER_TOKEN=$(kubectl get secrets ci-bot-token -o jsonpath="{.data.token}" | base64 -d)
export KKA_CLUSTER_CA_CERT=$(kubectl get secrets ci-bot-token -o jsonpath="{.data.ca\.crt}" | base64 -d)
export KKA_CLUSTER_TOKEN=$(kubectl get secrets kka-cli-token -o jsonpath="{.data.token}" | base64 -d)
export KKA_CLUSTER_CA_CERT=$(kubectl get secrets kka-cli-token -o jsonpath="{.data.ca\.crt}" | base64 -d)
```
17 changes: 7 additions & 10 deletions assets/github/onbranch.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,33 @@ on:

jobs:
on_pr:
concurrency: {{ `${{ github.head_ref }}` }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
ref: {{ `${{ github.head_ref }}` }}

- name: delete application
if: github.event.type == "closed"
if: github.event.action == 'closed'
uses: docker://ghcr.io/nearform/k8s-kurated-addons-cli:latest
with:
args: onbranch --clean
env:
KKA_APP_NAME: {{ .AppName }}
KKA_PROJECT_DIRECTORY: {{ .ProjectDirectory }}
KKA_REPO_NAME: {{ .Repository }}
KKA_REGISTRY_USER: {{ `${{ github.actor }}` }}
KKA_REGISTRY_PASSWORD: {{ `${{ secrets.GITHUB_TOKEN }}` }}
KKA_CLUSTER_ENDPOINT: {{ `${{ secrets.CLUSTER_ENDPOINT }}` }}
KKA_CLUSTER_TOKEN: {{ `${{ secrets.CLUSTER_TOKEN }}` }}
KKA_CLUSTER_CA_CERT: {{ `${{ secrets.CLUSTER_CA_CERT }}` }}

- name: build and deploy application
if: github.event.type != "closed"
if: github.event.action != 'closed'
uses: docker://ghcr.io/nearform/k8s-kurated-addons-cli:latest
with:
args: onbranch
env:
KKA_APP_NAME: {{ .AppName }}
KKA_PROJECT_DIRECTORY: {{ .ProjectDirectory }}
KKA_REPO_NAME: {{ .Repository }}
KKA_REGISTRY_USER: {{ `${{ github.actor }}` }}
KKA_REGISTRY_PASSWORD: {{ `${{ secrets.GITHUB_TOKEN }}` }}
KKA_CLUSTER_ENDPOINT: {{ `${{ secrets.CLUSTER_ENDPOINT }}` }}
KKA_CLUSTER_TOKEN: {{ `${{ secrets.CLUSTER_TOKEN }}` }}
KKA_CLUSTER_CA_CERT: {{ `${{ secrets.CLUSTER_CA_CERT }}` }}
KKA_CLUSTER_CA_CERT: {{ `${{ secrets.CLUSTER_CA_CERT }}` }}
3 changes: 0 additions & 3 deletions assets/github/onmain.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ jobs:
with:
args: onmain
env:
KKA_APP_NAME: {{ .AppName }}
KKA_PROJECT_DIRECTORY: {{ .ProjectDirectory }}
KKA_REPO_NAME: {{ .Repository }}
KKA_REGISTRY_USER: {{ `${{ github.actor }}` }}
KKA_REGISTRY_PASSWORD: {{ `${{ secrets.GITHUB_TOKEN }}` }}
KKA_CLUSTER_ENDPOINT: {{ `${{ secrets.CLUSTER_ENDPOINT }}` }}
Expand Down
26 changes: 26 additions & 0 deletions assets/k8s/serviceAccount/cluster-role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: kka-cli
rules:
- apiGroups:
- ''
- serving.knative.dev
- apps
- networking.k8s.io
resources:
- namespaces
- deployments
- replicasets
- ingresses
- services
- secrets
verbs:
- create
- delete
- deletecollection
- get
- list
- patch
- update
- watch
12 changes: 12 additions & 0 deletions assets/k8s/serviceAccount/role-binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kka-cli
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kka-cli
subjects:
- kind: ServiceAccount
name: kka-cli-bot
namespace: default
4 changes: 4 additions & 0 deletions assets/k8s/serviceAccount/service-account.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: kka-cli-bot
7 changes: 7 additions & 0 deletions assets/k8s/serviceAccount/token.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Secret
type: kubernetes.io/service-account-token
metadata:
name: kka-cli-token
annotations:
kubernetes.io/service-account.name: "kka-cli-bot"
1 change: 1 addition & 0 deletions assets/knative/service.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ spec:
spec:
containers:
- image: {{ .RemoteTag }}
imagePullPolicy: Always
6 changes: 5 additions & 1 deletion src/cli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import (
)

func (c *CLI) Build(cCtx *cli.Context) error {
project := c.getProject(cCtx)
project, err := c.getProject(cCtx)
if err != nil {
return err
}

logger.PrintInfo("Dockerfile Location: " + path.Join(project.Directory, c.DockerService.DockerFileName))
return c.DockerService.Build()
}
Expand Down
40 changes: 20 additions & 20 deletions src/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"embed"
"io"
"os"
"path"
"path/filepath"
"sort"

Expand All @@ -13,6 +12,7 @@ import (

"github.com/charmbracelet/log"
"github.com/nearform/k8s-kurated-addons-cli/src/services/docker"
"github.com/nearform/k8s-kurated-addons-cli/src/utils/defaults"
"github.com/nearform/k8s-kurated-addons-cli/src/utils/logger"
"github.com/urfave/cli/v2"
)
Expand All @@ -38,7 +38,7 @@ func (c CLI) baseBeforeFunc(ctx *cli.Context) error {
return nil
}

func (c *CLI) init(cCtx *cli.Context) {
func (c *CLI) init(cCtx *cli.Context) error {
appName := cCtx.String(appNameFlag)
version := cCtx.String(appVersionFlag)
projectDirectory := cCtx.String(projectDirectoryFlag)
Expand All @@ -59,9 +59,11 @@ func (c *CLI) init(cCtx *cli.Context) {

dockerImageName := appName
invalidBases := []string{".", string(os.PathSeparator)}
base := filepath.Base(absProjectDirectory)
if !slices.Contains(invalidBases, base) && base != dockerImageName {
dockerImageName = appName + "/" + base
if projectDirectory != defaults.ProjectDirectory {
base := filepath.Base(absProjectDirectory)
if !slices.Contains(invalidBases, base) && base != dockerImageName {
dockerImageName = appName + "/" + base
}
}

dockerImage := docker.DockerImage{
Expand All @@ -71,21 +73,30 @@ func (c *CLI) init(cCtx *cli.Context) {
Tag: version,
}

dockerService, err := docker.New(project, dockerImage, cCtx.String(dockerFileNameFlag))
dockerFileName := cCtx.String(dockerFileNameFlag)
if dockerFileName == "" {
dockerFileName = defaults.GeneratedDockerFile
}

dockerService, err := docker.New(project, dockerImage, dockerFileName)
if err != nil {
logger.PrintError("Error creating docker service", err)
}

c.DockerService = dockerService
c.dockerImage = dockerImage
c.project = project
return nil
}

func (c *CLI) getProject(cCtx *cli.Context) *project.Project {
func (c *CLI) getProject(cCtx *cli.Context) (*project.Project, error) {
if (c.project == project.Project{}) {
c.init(cCtx)
err := c.init(cCtx)
if err != nil {
return nil, err
}
}
return &c.project
return &c.project, nil
}

func (c CLI) Run(args []string) error {
Expand All @@ -108,17 +119,6 @@ func (c CLI) Run(args []string) error {
return err
}

projectDirectory := ctx.String(projectDirectoryFlag)
absProjectDirectory, err := filepath.Abs(projectDirectory)

if err != nil {
return err
}

if ctx.String(appNameFlag) == "" {
ctx.Set(appNameFlag, path.Base(absProjectDirectory))
}

if err := c.checkRequiredFlags(ctx, []string{}); err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion src/cli/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ func (c *CLI) Delete(cCtx *cli.Context) error {
if err != nil {
return err
}
project := c.getProject(cCtx)
project, err := c.getProject(cCtx)
if err != nil {
return err
}
return knative.Clean(cCtx.String(namespaceFlag), config, project)
}

Expand Down
5 changes: 4 additions & 1 deletion src/cli/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ func (c *CLI) Deploy(cCtx *cli.Context) error {
if err != nil {
return err
}
project := c.getProject(cCtx)
project, err := c.getProject(cCtx)
if err != nil {
return err
}

return knative.Apply(cCtx.String(namespaceFlag), config, project, c.dockerImage)
}
Expand Down
25 changes: 17 additions & 8 deletions src/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"strings"

"github.com/nearform/k8s-kurated-addons-cli/src/services/git"
"github.com/nearform/k8s-kurated-addons-cli/src/utils/defaults"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -46,7 +47,14 @@ const (
var requiredFlags []string
var flags map[FlagsType]([]cli.Flag)

// This function is executed when the module is loaded
func init() {
registry := ""
org, err := git.GetGithubOrg()
if err == nil {
registry = fmt.Sprintf("ghcr.io/%s", org)
}

defaultFlags := map[FlagsType]([]cli.Flag){
Build: []cli.Flag{
&cli.StringFlag{
Expand Down Expand Up @@ -111,9 +119,10 @@ func init() {
},
App: []cli.Flag{
&cli.StringFlag{
Name: appNameFlag,
Usage: "The name of the app",
EnvVars: []string{"KKA_APP_NAME"},
Name: appNameFlag,
Usage: "The name of the app",
Required: true,
EnvVars: []string{"KKA_APP_NAME"},
},
&cli.StringFlag{
Name: appVersionFlag,
Expand All @@ -128,15 +137,15 @@ func init() {
EnvVars: []string{"KKA_PROJECT_DIRECTORY"},
},
&cli.StringFlag{
Name: repoNameFlag,
Usage: "The base address of the container repository",
Value: defaults.RepoName,
EnvVars: []string{"KKA_REPO_NAME"},
Name: repoNameFlag,
Usage: "The base address of the container repository",
Value: registry,
Required: registry == "",
EnvVars: []string{"KKA_REPO_NAME"},
},
&cli.StringFlag{
Name: dockerFileNameFlag,
Usage: "The name of the Dockerfile",
Value: defaults.DockerfileName,
EnvVars: []string{"KKA_DOCKERFILE_NAME"},
},
&cli.StringFlag{
Expand Down
20 changes: 17 additions & 3 deletions src/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ import (
"github.com/charmbracelet/log"
"k8s.io/utils/strings/slices"

"github.com/nearform/k8s-kurated-addons-cli/src/services/k8s"
"github.com/nearform/k8s-kurated-addons-cli/src/services/project"
"github.com/urfave/cli/v2"
)

func (c CLI) InitGithubCMD(cCtx *cli.Context) error {
logger := log.New(os.Stderr)
logger.SetLevel(log.DebugLevel)
registry := cCtx.String(repoNameFlag)

options := project.InitOptions{
PipelineType: cCtx.Command.Name,
DestinationFolder: cCtx.String(destinationFolderFlag),
DefaultBranch: cCtx.String(defaultBranchFlag),
AppName: cCtx.String(appNameFlag),
Repository: cCtx.String(repoNameFlag),
Repository: registry,
ProjectDirectory: cCtx.String(projectDirectoryFlag),
}
data, err := project.ProjectInit(options, c.Resources)
Expand All @@ -39,6 +42,7 @@ func (c CLI) InitGithubCMD(cCtx *cli.Context) error {
func (c CLI) InitConfigCMD(ctx *cli.Context) error {
excludedFlags := []string{
"help",
appVersionFlag,
namespaceFlag,
configFileFlag,
projectDirectoryFlag,
Expand All @@ -61,19 +65,23 @@ func (c CLI) InitConfigCMD(ctx *cli.Context) error {

value := ctx.String(stringFlag.Name)
if value == "" {
stringFlag.GetValue()
value = stringFlag.Value
}

if value == "" {
fmt.Fprintf(c.Writer, "%s: null\n", stringFlag.Name)
} else {
fmt.Fprintf(c.Writer, "%s: %s\n", stringFlag.Name, ctx.String(stringFlag.Name))
fmt.Fprintf(c.Writer, "%s: %s\n", stringFlag.Name, value)
}
}

return nil
}

func (c CLI) InitServiceAccountCMD(ctx *cli.Context) error {
return k8s.GetServiceAccount(c.Resources)
}

func (c CLI) InitCMD() *cli.Command {
return &cli.Command{
Name: "init",
Expand All @@ -92,6 +100,12 @@ func (c CLI) InitCMD() *cli.Command {
Action: c.InitConfigCMD,
Before: c.baseBeforeFunc,
},
{
Name: "service-account",
Usage: "output all resources needed to create a service account",
Action: c.InitServiceAccountCMD,
Before: c.baseBeforeFunc,
},
},
}
}
Loading

0 comments on commit 315926b

Please sign in to comment.