Container build for Templater plugin

Change-Id: I53384561517070a0dca901ee263a5c1a1e55dffa
Relates-To: #341
This commit is contained in:
Sirajudeen 2020-10-15 15:16:42 +00:00
parent 6d6d837060
commit 909d427610
8 changed files with 242 additions and 0 deletions

View File

@ -0,0 +1,11 @@
FROM golang:1.13-stretch as builder
ENV CGO_ENABLED=0
WORKDIR /go/src/
COPY image/go.mod .
RUN go mod download
COPY main.go .
RUN go build -v -o /usr/local/bin/config-function ./
FROM alpine:latest
COPY --from=builder /usr/local/bin/config-function /usr/local/bin/config-function
CMD ["/usr/local/bin/config-function"]

View File

@ -0,0 +1,78 @@
.PHONY: generate license fix vet fmt test build tidy image
SHELL := /bin/bash
GOBIN := $(shell go env GOPATH)/bin
# docker image options
DOCKER_REGISTRY ?= quay.io
DOCKER_IMAGE_NAME ?= templater
DOCKER_IMAGE_PREFIX ?= airshipit
DOCKER_IMAGE_TAG ?= dev
DOCKER_IMAGE ?= $(DOCKER_REGISTRY)/$(DOCKER_IMAGE_PREFIX)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)
PUBLISH ?= false
DOCKER_FORCE_CLEAN ?= true
# proxy options
PROXY ?= http://proxy.foo.com:8000
NO_PROXY ?= localhost,127.0.0.1,.svc.cluster.local
USE_PROXY ?= false
.PHONY: build
build:
(cd image && go build -v -o $(GOBIN)/config-function .)
.PHONY: all
all: generate license build fix vet fmt test lint tidy
.PHONY: fix
fix:
(cd image && go fix ./...)
.PHONY: fmt
fmt:
(cd image && go fmt ./...)
.PHONY: generate
generate:
(which $(GOBIN)/mdtogo || go get sigs.k8s.io/kustomize/cmd/mdtogo)
(cd image && GOBIN=$(GOBIN) go generate ./...)
.PHONY: tidy
tidy:
(cd image && go mod tidy)
.PHONY: fix
lint:
(which $(GOBIN)/golangci-lint || go get github.com/golangci/golangci-lint/cmd/golangci-lint@v1.19.1)
(cd image && $(GOBIN)/golangci-lint run ./...)
.PHONY: test
test:
(cd image && go test -cover ./...)
.PHONY: vet
vet:
(cd image && go vet ./...)
.PHONY: image
image:
ifeq ($(USE_PROXY), true)
cd image && \
docker build . --network=host \
--build-arg http_proxy=$(PROXY) \
--build-arg https_proxy=$(PROXY) \
--build-arg HTTP_PROXY=$(PROXY) \
--build-arg HTTPS_PROXY=$(PROXY) \
--build-arg no_proxy=$(NO_PROXY) \
--build-arg NO_PROXY=$(NO_PROXY) \
--tag $(DOCKER_IMAGE) \
--force-rm=$(DOCKER_FORCE_CLEAN)
else
cd image && \
docker build . --network=host \
--tag $(DOCKER_IMAGE) \
--force-rm=$(DOCKER_FORCE_CLEAN)
endif
ifeq ($(PUBLISH), true)
@docker push $(DOCKER_IMAGE)
endif

View File

@ -0,0 +1,71 @@
# Templater function
This plugin is an implementation of a templater function written using `go` and uses the `kyaml`
and `airshipctl` libraries for parsing the input and writing the output.
## Function implementation
The function is implemented as an [image](image), and built using `make image`.
## Function invocation
The function is invoked by authoring a [Local Resource](local-resource)
with `metadata.annotations.[config.kubernetes.io/function]` and running:
kustomize fn run local-resource/
This exits non-zero if there is an error.
## Running the Example
Run the function with:
kustomize fn run local-resource/
The generated resources will appear in local-resource/
```
$ cat local-resource/*
apiVersion: metal3.io/v1alpha1
kind: BareMetalHost
metadata:
name: node-1
spec:
bootMACAddress: 00:aa:bb:cc:dd
apiVersion: metal3.io/v1alpha1
kind: BareMetalHost
metadata:
name: node-2
spec:
bootMACAddress: 00:aa:bb:cc:ee
...
```
### Configuration file format
`Templater` configuration resource is represented as a standard
k8s resource with Group, Version, Kind and Metadata header. Templater
configuration is defined using `template` and `values` fields with
following structure.
values:
hosts:
- macAddress: 00:aa:bb:cc:dd
name: node-1
- macAddress: 00:aa:bb:cc:ee
name: node-2
template: |
{{ range .hosts -}}
---
apiVersion: metal3.io/v1alpha1
kind: BareMetalHost
metadata:
name: {{ .name }}
spec:
bootMACAddress: {{ .macAddress }}
{{ end -}}
`values` defines the substituion value as Map.
`template` defines the template with placeholders to substitue the value from the
values Map

View File

@ -0,0 +1,8 @@
module opendev.org/airship/airshipctl/functions/templater/image
go 1.14
require (
opendev.org/airship/airshipctl v0.0.0-20201007215749-76e4d3f48c5a
sigs.k8s.io/kustomize/kyaml v0.7.1
)

View File

@ -0,0 +1,34 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: airshipit.org/v1alpha1
kind: Templater
metadata:
annotations:
config.kubernetes.io/function: |
container:
image: quay.io/airshipit/templater:dev
values:
hosts:
- macAddress: 00:aa:bb:cc:dd
name: node-1
- macAddress: 00:aa:bb:cc:ee
name: node-2
template: |
{{ range .hosts -}}
---
apiVersion: metal3.io/v1alpha1
kind: BareMetalHost
metadata:
name: {{ .name }}
spec:
bootMACAddress: {{ .macAddress }}
{{ end -}}

View File

@ -0,0 +1,32 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
// Package main implements an injection function for resource reservations and
// is run with `kustomize config run -- DIR/`.
package main
import (
"fmt"
"os"
"sigs.k8s.io/kustomize/kyaml/fn/framework"
"opendev.org/airship/airshipctl/pkg/document/plugin/templater"
)
func main() {
cfg := make(map[string]interface{})
resourceList := &framework.ResourceList{FunctionConfig: &cfg}
cmd := framework.Command(resourceList, func() error {
plugin, err := templater.New(cfg)
if err != nil {
return err
}
resourceList.Items, err = plugin.Filter(resourceList.Items)
return err
})
if err := cmd.Execute(); err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}

View File

@ -4,6 +4,10 @@ metadata:
name: env-vars-template name: env-vars-template
labels: labels:
airshipit.org/deploy-k8s: "false" airshipit.org/deploy-k8s: "false"
annotations:
config.kubernetes.io/function: |-
container:
image: quay.io/airshipit/templater:dev
template: | template: |
--- ---
apiVersion: airshipit.org/v1alpha1 apiVersion: airshipit.org/v1alpha1

View File

@ -2,6 +2,10 @@ apiVersion: airshipit.org/v1alpha1
kind: Templater kind: Templater
metadata: metadata:
name: m3-host-template name: m3-host-template
annotations:
config.kubernetes.io/function: |-
container:
image: quay.io/airshipit/templater:dev
values: values:
# hosts: # hosts: