Skip to content

Commit

Permalink
helm provider config, new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arti-shalb committed Jun 18, 2024
1 parent 20b5fa1 commit d4e499b
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 736 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/pr_tests copy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Pr Tests
on:
push:
branches:
- cdev-ignore

jobs:
tests:
name: Build and run tests
runs-on: ubuntu-latest
container: golang:1.21.3-alpine
steps:
- name: Code checkout
uses: actions/checkout@v2

- name: Build image
run: |
set -x
ls -la
docker ps
44 changes: 29 additions & 15 deletions internal/units/shell/terraform/helm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/shalb/cluster.dev/internal/project"
"github.com/shalb/cluster.dev/internal/units/shell/common"
"github.com/shalb/cluster.dev/internal/units/shell/terraform/base"
"github.com/shalb/cluster.dev/internal/units/shell/terraform/types"
"github.com/shalb/cluster.dev/pkg/hcltools"
"github.com/shalb/cluster.dev/pkg/utils"
"github.com/zclconf/go-cty/cty"
Expand All @@ -21,15 +22,16 @@ import (

type Unit struct {
base.Unit
Source string `yaml:"-" json:"source"`
HelmOpts map[string]interface{} `yaml:"-" json:"helm_opts,omitempty"`
Sets map[string]interface{} `yaml:"-" json:"sets,omitempty"`
Kubeconfig *string `yaml:"-" json:"kubeconfig"`
ValuesFilesList []string `yaml:"-" json:"values,omitempty"`
ValuesYAML []map[string]interface{} `yaml:"-" json:"-"`
UnitKind string `yaml:"-" json:"type"`
StateData project.Unit `yaml:"-" json:"-"`
CustomFiles *common.FilesListT `yaml:"create_files,omitempty" json:"create_files,omitempty"`
Source string `yaml:"-,omitempty" json:"source"`
HelmOpts map[string]interface{} `yaml:"-" json:"helm_opts,omitempty"`
Sets map[string]interface{} `yaml:"-" json:"sets,omitempty"`
Kubeconfig *string `yaml:"-" json:"kubeconfig"`
ValuesFilesList []string `yaml:"-" json:"values,omitempty"`
ValuesYAML []map[string]interface{} `yaml:"-" json:"-"`
UnitKind string `yaml:"-" json:"type"`
StateData project.Unit `yaml:"-" json:"-"`
CustomFiles *common.FilesListT `yaml:"create_files,omitempty" json:"create_files,omitempty"`
ProviderConf *types.ProviderConfigSpec `yaml:"provider_conf" json:"provider_conf"`
}

func (u *Unit) KindKey() string {
Expand All @@ -44,13 +46,22 @@ func (u *Unit) genMainCodeBlock() ([]byte, error) {
f := hclwrite.NewEmptyFile()
providerBody := &hclwrite.Body{}
rootBody := f.Body()
if u.Kubeconfig != nil {
if u.ProviderConf != nil || u.Kubeconfig != nil {
providerBlock := rootBody.AppendNewBlock("provider", []string{"helm"})
providerBody = providerBlock.Body()
provederKubernetesBlock := providerBody.AppendNewBlock("kubernetes", []string{})
provederKubernetesBlock.Body().SetAttributeValue("config_path", cty.StringVal(*u.Kubeconfig))
if config.Global.LogLevel == "debug" {
providerBody.SetAttributeValue("debug", cty.BoolVal(true))
providerKubernetesBlock := providerBody.AppendNewBlock("kubernetes", []string{})
if u.ProviderConf != nil {
providerCty, err := hcltools.InterfaceToCty(*u.ProviderConf)
if err != nil {
return nil, err
}
for key, val := range providerCty.AsValueMap() {
providerKubernetesBlock.Body().SetAttributeValue(key, val)
}
}
if u.Kubeconfig != nil {
log.Warn("Deprecation warning: helm unit option 'kubeconfig' is deprecated. Please use 'provider_conf.config_path' instead.")
providerKubernetesBlock.Body().SetAttributeValue("config_path", cty.StringVal(*u.Kubeconfig))
}
}
helmBlock := rootBody.AppendNewBlock("resource", []string{"helm_release", project.ConvertToTfVarName(u.Name())})
Expand Down Expand Up @@ -91,7 +102,10 @@ func (u *Unit) genMainCodeBlock() ([]byte, error) {
}

func (u *Unit) ReadConfig(spec map[string]interface{}, stack *project.Stack) error {

err := utils.YAMLInterfaceToType(spec, u)
if err != nil {
return fmt.Errorf("read 'provider_conf': %w", err)
}
source, ok := spec["source"].(map[string]interface{})
if !ok {
return fmt.Errorf("read unit config: incorrect unit source, %v", u.Key())
Expand Down
30 changes: 5 additions & 25 deletions internal/units/shell/terraform/kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/shalb/cluster.dev/pkg/hcltools"

"github.com/shalb/cluster.dev/internal/project"
"github.com/shalb/cluster.dev/internal/units/shell/terraform/types"
"github.com/shalb/cluster.dev/pkg/utils"
)

Expand All @@ -22,30 +23,9 @@ type Unit struct {
Kubeconfig string `yaml:"-" json:"kubeconfig"`
Inputs map[string]interface{} `yaml:"-" json:"inputs"`
// providerVersion string `yaml:"-" json:"-"`
ProviderConf ProviderConfigSpec `yaml:"-" json:"provider_conf"`
UnitKind string `yaml:"-" json:"type"`
StateData project.Unit `yaml:"-" json:"-"`
}

type ExecNestedSchema struct {
APIVersion string `yaml:"api_version,omitempty" json:"api_version,omitempty"`
Args []string `yaml:"args,omitempty" json:"args,omitempty"`
Command string `yaml:"command,omitempty" json:"command,omitempty"`
Env map[string]string `yaml:"env,omitempty" json:"env,omitempty"`
}

type ProviderConfigSpec struct {
ConfigPath string `yaml:"config_path,omitempty" json:"config_path,omitempty"`
ClientCertificate string `yaml:"client_certificate,omitempty" json:"client_certificate,omitempty"`
ConfigContext string `yaml:"config_context,omitempty" json:"config_context,omitempty"`
ConfigContextCluster string `yaml:"config_context_cluster,omitempty" json:"config_context_cluster,omitempty"`
ConfigContextUser string `yaml:"config_context_user,omitempty" json:"config_context_user,omitempty"`
Exec *ExecNestedSchema `yaml:"exec,omitempty" json:"exec,omitempty"`
Host string `yaml:"host,omitempty" json:"host,omitempty"`
Insecure string `yaml:"insecure,omitempty" json:"insecure,omitempty"`
Password string `yaml:"password,omitempty" json:"password,omitempty"`
Token string `yaml:"token,omitempty" json:"token,omitempty"`
Username string `yaml:"username,omitempty" json:"username,omitempty"`
ProviderConf types.ProviderConfigSpec `yaml:"provider_conf" json:"provider_conf"`
UnitKind string `yaml:"-" json:"type"`
StateData project.Unit `yaml:"-" json:"-"`
}

func (u *Unit) KindKey() string {
Expand Down Expand Up @@ -139,7 +119,7 @@ func (u *Unit) ReadConfig(spec map[string]interface{}, stack *project.Stack) err
return fmt.Errorf("the kubernetes unit must contain at least one manifest")
}

err = utils.JSONCopy(spec, &u.ProviderConf)
err = utils.YAMLInterfaceToType(spec, u)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions internal/units/shell/terraform/kubernetes/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/shalb/cluster.dev/internal/project"
"github.com/shalb/cluster.dev/internal/units/shell/terraform/base"
"github.com/shalb/cluster.dev/internal/units/shell/terraform/types"
"github.com/shalb/cluster.dev/pkg/utils"
)

Expand All @@ -29,8 +30,8 @@ func (u *Unit) GetState() project.Unit {

type UnitDiffSpec struct {
base.UnitDiffSpec
ProviderConf ProviderConfigSpec `json:"provider_conf"`
Inputs interface{} `json:"inputs"`
ProviderConf types.ProviderConfigSpec `json:"provider_conf"`
Inputs interface{} `json:"inputs"`
}

func (u *Unit) GetUnitDiff() UnitDiffSpec {
Expand Down
30 changes: 30 additions & 0 deletions internal/units/shell/terraform/types/kube_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package types

type ExecNestedSchema struct {
APIVersion string `yaml:"api_version,omitempty" json:"api_version,omitempty"`
Args []string `yaml:"args,omitempty" json:"args,omitempty"`
Command string `yaml:"command,omitempty" json:"command,omitempty"`
Env map[string]string `yaml:"env,omitempty" json:"env,omitempty"`
}

type ProviderConfigSpec struct {
ConfigPath string `yaml:"config_path,omitempty" json:"config_path,omitempty"`
ConfigPaths []string `yaml:"config_paths,omitempty" json:"config_paths,omitempty"`
ClientCertificate string `yaml:"client_certificate,omitempty" json:"client_certificate,omitempty"`
ClientKey string `yaml:"client_key,omitempty" json:"client_key,omitempty"`
ClusterCaCertificate string `yaml:"cluster_ca_certificate,omitempty" json:"cluster_ca_certificate,omitempty"`
ConfigContext string `yaml:"config_context,omitempty" json:"config_context,omitempty"`
ConfigContextCluster string `yaml:"config_context_cluster,omitempty" json:"config_context_cluster,omitempty"`
ConfigContextUser string `yaml:"config_context_user,omitempty" json:"config_context_user,omitempty"`
ConfigContextAuthInfo string `yaml:"config_context_auth_info,omitempty" json:"config_context_auth_info,omitempty"`
ProxyURL string `yaml:"proxy_url,omitempty" json:"proxy_url,omitempty"`
Exec *ExecNestedSchema `yaml:"exec,omitempty" json:"exec,omitempty"`
Host string `yaml:"host,omitempty" json:"host,omitempty"`
Insecure string `yaml:"insecure,omitempty" json:"insecure,omitempty"`
Password string `yaml:"password,omitempty" json:"password,omitempty"`
Token string `yaml:"token,omitempty" json:"token,omitempty"`
Username string `yaml:"username,omitempty" json:"username,omitempty"`
IgnoreAnnotations string `yaml:"ignore_annotations,omitempty" json:"ignore_annotations,omitempty"`
IgnoreLabels string `yaml:"ignore_labels,omitempty" json:"ignore_labels,omitempty"`
TlsServerName string `yaml:"tls_server_name,omitempty" json:"tls_server_name,omitempty"`
}
1 change: 1 addition & 0 deletions tests/k8s/kube-helm/.cdevignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deployment.yaml
21 changes: 21 additions & 0 deletions tests/k8s/kube-helm/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
44 changes: 44 additions & 0 deletions tests/k8s/kube-helm/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
_p: &provider_aws
- aws:
region: {{ .variables.region }}

name: local-template
kind: StackTemplate
cliVersion: "~>0.9.2"
units:
-
name: consul
type: helm
source:
repository: "https://charts.bitnami.com/bitnami"
chart: "consul"
version: "10.9.12"
kubeconfig: /home/kk
provider_conf:
config_path: /home/user/.kube/config
cluster_ca_certificate: |
asdasd
asd
asd
asdasdasd
additional_options:
namespace: default
create_namespace: true
values:
- file: ./values/consul.yaml
-
name: cert-manager-issuer
type: kubernetes
source: ./deployment.yaml
provider_conf:
config_paths:
- ./deployment.yaml
host: k8s.example.com
username: "user"
password: "secretPassword"
-
name: outputs
type: printer
outputs:
new_output: "test2"

1 change: 1 addition & 0 deletions tests/k8s/kube-helm/values/consul.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kind: deployment
7 changes: 7 additions & 0 deletions tests/k8s/project.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: dev
kind: Project
backend: default
variables:
organization: cluster-dev
exports:
CDEV_COLLECT_USAGE_STATS: "false"
7 changes: 7 additions & 0 deletions tests/k8s/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: kube-helm
template: ./kube-helm/
kind: Stack
disabled: false
backend: default
variables: {}

20 changes: 0 additions & 20 deletions tests/test-project/README.md

This file was deleted.

Loading

0 comments on commit d4e499b

Please sign in to comment.