Skip to content

Commit

Permalink
Merge pull request #1 from HamzaZo/feat/add-plugin
Browse files Browse the repository at this point in the history
init commit
  • Loading branch information
HamzaZo authored May 16, 2021
2 parents eb2333a + 8d6b67c commit 830da55
Show file tree
Hide file tree
Showing 19 changed files with 2,264 additions and 1 deletion.
19 changes: 19 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Run a one-line script
run: make build
30 changes: 30 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: goreleaser

on:
push:
tags:
- 'v*.*.*'
workflow_dispatch:

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20 changes: 20 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
builds:
- main: main.go
binary: adopt
env:
- CGO_ENABLED=0
goos:
- darwin
- linux
goarch:
- amd64
- arm64
archives:
- id: archive
format: tar.gz
files:
- README.md
- LICENSE
- plugin.yaml
- completion.yaml
- scripts/install_plugin.sh
21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

.PYHONY: build
# Build plugin binary
build: fmt vet
CGO_ENABLED=0 GO111MODULE=on go build -o bin/adopt ./main.go

# Run go fmt against code
.PYHONY: fmt
fmt:
go fmt ./...

# Run go vet against code
.PYHONY: vet
vet:
go vet ./...
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
[![Licence](https://img.shields.io/badge/licence-Apache%202.0-green)]()
[![Helm](https://img.shields.io/badge/release-0.1.0-brightgreen)]()

# helm-adopt
A helm v3 plugin to adopt k8s resources into a helm chart
`helm-adopt` is a helm plugin to adopt k8s resources into a helm chart.

## Getting started

### Installation

To install the plugin:
```shell
$ helm plugin install https://github.com/HamzaZo/helm-adopt
```
Update to latest
```shell
$ helm plugin update adopt
```
Install a specific version
```shell
$ helm plugin install https://github.com/HamzaZo/helm-adopt --version 0.1.0
```
You can also verify it's been installed using
```shell
$ helm plugin list
```

### Usage
TODO
144 changes: 144 additions & 0 deletions cmd/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package cmd

import (
"fmt"
"github.com/HamzaZo/helm-adopt/internal/discovery"
"github.com/HamzaZo/helm-adopt/internal/generate"
"github.com/HamzaZo/helm-adopt/internal/utils"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"helm.sh/helm/v3/cmd/helm/require"
"io"
)

var (
chartDir string
releaseName string
dryRun bool
debug bool
)

type EnvSettings struct {
KubeConfigFile string
KubeContext string
Namespace string
}

const basicExample = `
Adopt k8s resources into a new generated helm chart
Examples:
$ %[1]s adopt resources deployments:nginx services:my-svc -o/--output frontend
$ %[1]s adopt resources deployments:nginx clusterrolebindings:binding-rbac -o/--output frontend -n/--namespace <ns>
$ %[1]s adopt resources statefulsets:nginx services:my-svc -r/--release RELEASE-NAME -o/--output frontend -c/--kube-context <ctx>
$ %[1]s adopt resources deployments:nginx services:my-svc -r/--release RELEASE-NAME -o/--output frontend -k/--kubeconfig <kcfg>
`

func NewResourcesCmd(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "resources <pluralKind>:<name>",
Short: "adopt k8s resources into a new generated helm chart",
Long: fmt.Sprintf(basicExample, "helm"),
Args: require.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
err := runResources(args, out)
if err != nil{
return err
}
return nil
},

}
flags := cmd.Flags()

flags.StringVarP(&chartDir, "output", "o", "", "Specify the chart directory of loaded yaml files")
cmd.MarkFlagRequired("output")
flags.StringVarP(&releaseName, "release", "r", "", "Specify the name for the generated release")
flags.BoolVar(&dryRun, "dry-run", false, "Print what resources will be adopted ")
flags.BoolVar(&debug, "debug", false, "Show the generated manifests on STDOUT")

Settings.AddFlags(flags)

return cmd
}

// AddFlags binds flags to the given flagset
func (e *EnvSettings) AddFlags(fs *pflag.FlagSet) {
fs.StringVarP(&e.KubeConfigFile, "kubeconfig", "k", "", "path to the kubeconfig file")
fs.StringVarP(&e.KubeContext, "kube-context", "c", e.KubeContext, "name of the kubeconfig context to use")
fs.StringVarP(&e.Namespace, "namespace", "n", e.Namespace, "namespace scope for this request")

}


//runResources adopt given k8s resources into a helm chart
func runResources(args []string, out io.Writer) error{
if releaseName == "" {
releaseName = "generated-release"
}
err := utils.ChartValidator(chartDir,releaseName)
if err != nil {
return err
}
log.Info("Adopting resources..")

input, err := utils.GetAllArgs(args)
if err != nil {
return err
}

kubeconfig := discovery.KubConfigSetup{
Context: Settings.KubeContext,
KubeConfigFile: Settings.KubeConfigFile,
Namespace: Settings.Namespace,
}
helmClient, err := discovery.NewHelmClient(kubeconfig, kubeconfig.Namespace)
if err != nil {
return err
}

content, err := fetchResources(helmClient, input)
if err != nil {
return err
}
chart := &generate.Chart{
ChartName: chartDir,
ReleaseName: releaseName,
Content: content,
}

err = chart.Generate(helmClient, out, dryRun, debug)
if err != nil {
return err
}

return nil
}

//fetchResources get namespaced and non-namespaced resources.
func fetchResources(client *discovery.ApiClient, input map[string][]string) (map[string][]byte, error){
var output map[string][]byte

namespaceResource, clusterResource ,err := discovery.FetchedFilteredResources(client, input)
if err != nil {
return nil, err
}

ns , err := namespaceResource.Query(client, client.Namespace)
if err != nil {
return nil, err
}

cls , err := clusterResource.Query(client, "")
if err != nil {
return nil, err
}
output = utils.MergeMapsBytes(ns, cls)

return output, err
}
40 changes: 40 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmd

import (
"errors"
"github.com/spf13/cobra"
"io"
"os"
)

const globalUsage = `
Adopt k8s resources into a new helm chart. It's expects to match plural resources kinds.
`

var Settings *EnvSettings


func NewRootCmd(out io.Writer, args []string) *cobra.Command{
cmd := &cobra.Command{
Use: "adopt",
Short: "adopt cluster resources into a new helm chart",
Long: globalUsage,
SilenceUsage: true,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return errors.New("no argument accepted")
}
return nil
},
}
flags := cmd.PersistentFlags()
flags.Parse(args)

Settings = new(EnvSettings)
if ctx := os.Getenv("HELM_KUBECONTEXT"); ctx != ""{
Settings.KubeContext = ctx
}
cmd.AddCommand(NewResourcesCmd(out))

return cmd
}
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/HamzaZo/helm-adopt

go 1.16

require (
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
helm.sh/helm/v3 v3.4.1
k8s.io/apimachinery v0.20.0
k8s.io/client-go v0.20.0
k8s.io/kubectl v0.20.0 // indirect
rsc.io/letsencrypt v0.0.3 // indirect
sigs.k8s.io/yaml v1.2.0
)
Loading

0 comments on commit 830da55

Please sign in to comment.