Skip to content

Commit

Permalink
Merge pull request #31 from seungkyua/20220530_upgrade_transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
bluejayA authored Jun 24, 2022
2 parents 6d7131b + 934d48d commit a857d52
Show file tree
Hide file tree
Showing 9 changed files with 675 additions and 451 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/run-kustomize.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ jobs:
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.14
- name: Set up Go 1.16
uses: actions/setup-go@v2
with:
go-version: 1.14
go-version: 1.16

- name: Check out code into the Go module directory
uses: actions/checkout@v2
Expand All @@ -23,8 +23,7 @@ jobs:
env:
GO111MODULE: "on"
run: |
kustomize_version=$(sed -nr 's/\* kustomize [^0-9]*(([0-9]+\.)*[0-9]+).*/\1/p' README.md)
go get sigs.k8s.io/kustomize/kustomize/v3@v$kustomize_version
go get sigs.k8s.io/kustomize/kustomize/[email protected]
- name: Build
run: |
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
vendor/
68 changes: 49 additions & 19 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,31 +1,61 @@
FROM alpine:latest

MAINTAINER SKTelecom 5GX Cloud Labs
FROM golang:1.16.15-alpine3.15 AS builder
LABEL AUTHOR Seungkyu Ahn ([email protected])

ENV KUSTOMIZE_VER v4.2.0
ENV HOME /root
ENV GO111MODULE on
ENV PATH /usr/local/go/bin:$PATH
ENV GOROOT /usr/local/go
ENV GOPATH $HOME/golang
# on means using vendor directory
ENV GO111MODULE on
# CGO_ENABLED=1 default value means using link
ENV CGO_ENABLED 1

RUN mkdir -p $HOME/golang
RUN mkdir -p $HOME/.config/kustomize/plugin/openinfradev.github.com/v1/helmvaluestransformer

RUN apk update && apk add --no-cache curl git jq openssh libc6-compat build-base bash
RUN apk update && apk add git curl tar bash build-base


WORKDIR $HOME
COPY . $HOME/kustomize-helm-transformer
RUN cat kustomize-helm-transformer/README.md | grep -m 1 "* kustomize" | sed -nre 's/^[^0-9]*(([0-9]+\.)*[0-9]+).*/\1/p' > .kustomize_version
RUN cat kustomize-helm-transformer/README.md | grep -m 1 "* go" | sed -nre 's/^[^0-9]*(([0-9]+\.)*[0-9]+).*/\1/p' > .golang_version

WORKDIR /usr/local
RUN curl -fL https://dl.google.com/go/go$(cat $HOME/.golang_version).linux-amd64.tar.gz | tar xz

RUN go get sigs.k8s.io/kustomize/kustomize/v3@v$(cat $HOME/.kustomize_version)
RUN mv $GOPATH/bin/kustomize /usr/local/bin/
# install kustomize from source
WORKDIR $HOME
RUN git clone https://github.com/kubernetes-sigs/kustomize.git
WORKDIR $HOME/kustomize
RUN git checkout -b tags_${KUSTOMIZE_VER} tags/kustomize/${KUSTOMIZE_VER}
WORKDIR $HOME/kustomize/kustomize
RUN unset GOPATH && unset GO111MODULES && go install .
RUN cp $HOME/go/bin/kustomize /usr/local/bin/


# plugin copy
RUN rm -rf $HOME/kustomize/plugin/*
RUN cp -r $HOME/kustomize-helm-transformer/plugin/openinfradev.github.com $HOME/kustomize/plugin/


WORKDIR $HOME/kustomize/plugin/openinfradev.github.com/v1/helmvaluestransformer
RUN rm -rf vendor && rm -f go.mod go.sum
RUN mv kustomize-${KUSTOMIZE_VER}-go.mod go.mod
# update go.mod and go.sum
RUN unset GOPATH && unset GO111MODULES && go mod tidy


WORKDIR $HOME/kustomize
RUN unset GOPATH && unset GO111MODULES && ./hack/buildExternalGoPlugins.sh ./plugin





FROM alpine:edge
LABEL AUTHOR Seungkyu Ahn ([email protected])

RUN apk add --no-cache bash git

USER root

WORKDIR $HOME/kustomize-helm-transformer/plugin/openinfradev.github.com/v1/helmvaluestransformer/
RUN go test
RUN mv HelmValuesTransformer.so $HOME/.config/kustomize/plugin/openinfradev.github.com/v1/helmvaluestransformer/
RUN mkdir -p /root/.config/kustomize/plugin/openinfradev.github.com/v1/helmvaluestransformer
COPY --from=builder /root/kustomize/plugin/openinfradev.github.com/v1/helmvaluestransformer/HelmValuesTransformer.so /root/.config/kustomize/plugin/openinfradev.github.com/v1/helmvaluestransformer/
COPY --from=builder /usr/local/bin/kustomize /usr/local/bin/kustomize
WORKDIR /root

WORKDIR /
CMD ["kustomize"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Please take a look at the [example](https://github.com/openinfradev/kustomize-he


## Support
* kustomize v3.8.7
* go 1.14
* kustomize v4.2.0
* go 1.16.15

## Features
1. Replaced values of HelmRelease CustomResource using inline path
Expand Down
38 changes: 38 additions & 0 deletions examples/output/helm.fluxcd.io_v1_helmrelease_glance.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
apiVersion: helm.fluxcd.io/v1
kind: HelmRelease
metadata:
name: glance
spec:
chart:
name: glance
repository: http://helm-chart-repository
version: 1.0.0
releaseName: glance
targetNamespace: openstack
values:
bootstrap:
enabled: true
structured:
images:
cirros:
id: 201084fc-c276-4744-8504-cb974dbb3610
image_file: cirros-0.4.0-x86_64-disk.img
name: Cirros 0.4.0 64-bit
private: false
source_url: http://download.cirros-cloud.net/0.4.0/
conf:
ceph:
admin_keyring: admin_keyring
enabled: true
glance:
DEFAULT:
show_image_direct_url: true
show_multiple_locations: true
glance_store:
rbd_store_pool: images
rbd_store_user: glance
volumeClaimTemplates:
- metadata:
name: elasticsearch-data
spec:
storageClassName: ceph
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
package main

import (
"errors"
"fmt"
"log"
"os"
"reflect"
"regexp"
"strings"

"github.com/pkg/errors"
"sigs.k8s.io/kustomize/api/filters/patchstrategicmerge"
"sigs.k8s.io/kustomize/api/resid"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/kyaml/filtersutil"
"sigs.k8s.io/kustomize/kyaml/yaml"
"sigs.k8s.io/kustomize/kyaml/resid"
"sigs.k8s.io/yaml"
)

// Override values in HelmReleases
Expand All @@ -31,7 +31,7 @@ type plugin struct {
// ReplacedChart is including target information and chart values to override
type ReplacedChart struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Source ChartSource `json:"source,omitemty" yaml:"source,omitempty"`
Source ChartSource `json:"source,omitempty" yaml:"source,omitempty"`
Override map[string]interface{} `json:"override,omitempty" yaml:"override,omitempty"`
}

Expand Down Expand Up @@ -78,9 +78,16 @@ func (p *plugin) Transform(m resmap.ResMap) (err error) {
p.Logger.Println("Can't find HelmRelease name: " + chart.Name)
continue
}
if err := p.replaceChartSource(origin.Map(), chart.Source); err != nil {

overrideChartResource, err := p.getChartResource(chart.Source)
if err != nil {
return err
}
err = p.applyPatch(origin, overrideChartResource)
if err != nil {
return err
}

overrideResource, err := p.getResourceFromChart(chart)
if err != nil {
return err
Expand All @@ -103,48 +110,54 @@ func (p *plugin) applyPatch(resource, patch *resource.Resource) error {
err = filtersutil.ApplyToJSON(patchstrategicmerge.Filter{
Patch: node,
}, resource)
if !resource.IsEmpty() {
if !resource.IsNilOrEmpty() {
resource.SetName(n)
resource.SetNamespace(ns)
}
return err
}

func (p *plugin) replaceChartSource(origin map[string]interface{}, chartSource ChartSource) (err error) {
releaseSpec := origin["spec"].(map[string]interface{})
chart := releaseSpec["chart"].(map[string]interface{})
func (p *plugin) getChartResource(chartSource ChartSource) (r *resource.Resource, err error) {
patchChartMap := map[string]interface{}{}
if chartSource.Repository != "" {
repository, err := p.replaceGlobalVar(chartSource.Repository)
if err != nil {
return err
return nil, err
}
chart["repository"] = repository
patchChartMap["repository"] = repository
}

if chartSource.Version != "" {
version, err := p.replaceGlobalVar(chartSource.Version)
if err != nil {
return err
return nil, err
}
chart["version"] = version
patchChartMap["version"] = version
}

if chartSource.Name != "" {
name, err := p.replaceGlobalVar(chartSource.Name)
if err != nil {
return err
return nil, err
}
chart["name"] = name
patchChartMap["name"] = name
}

if chartSource.Type != "" {
chartType, err := p.replaceGlobalVar(chartSource.Type)
if err != nil {
return err
return nil, err
}
chart["type"] = chartType
patchChartMap["type"] = chartType
}
return nil

resource := p.h.ResmapFactory().RF().FromMap(map[string]interface{}{
"spec": map[string]interface{}{
"chart": patchChartMap,
},
})

return resource, nil
}

func (p *plugin) getResourceFromChart(replacedChart ReplacedChart) (r *resource.Resource, err error) {
Expand Down Expand Up @@ -198,7 +211,7 @@ func (p *plugin) replaceGlobalVar(original interface{}) (interface{}, error) {
isMatched := re.MatchString(inlineStr)

// no global variable
if isMatched == false {
if !isMatched {
return original, nil
}

Expand Down
31 changes: 18 additions & 13 deletions plugin/openinfradev.github.com/v1/helmvaluestransformer/go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
module github.com/openinfradev/kustomize-helm-transformer/openinfradev.github.com/v1
module github.com/openinfradev/kustomize-helm-transformer/plugin/openinfradev.github.com/v1/helmvaluestransformer

go 1.14
go 1.16

require (
github.com/google/go-cmp v0.5.2 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/stretchr/testify v1.6.1 // indirect
golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
k8s.io/client-go v0.18.10 // indirect
sigs.k8s.io/kustomize/api v0.6.5
sigs.k8s.io/kustomize/kyaml v0.9.4
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/pkg/errors v0.9.1
github.com/xlab/treeprint v1.1.0 // indirect
go.starlark.net v0.0.0-20220328144851-d1966c6b9fcd // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/kube-openapi v0.0.0-20220413171646-5e7f5fdc6da6 // indirect
sigs.k8s.io/kustomize/api v0.11.5
sigs.k8s.io/kustomize/kyaml v0.13.7
sigs.k8s.io/yaml v1.3.0
)
Loading

0 comments on commit a857d52

Please sign in to comment.