Skip to content

Commit

Permalink
Add proxy mode to install charts with the help of an operator
Browse files Browse the repository at this point in the history
  • Loading branch information
kramerul committed Dec 23, 2019
1 parent 3d12b44 commit 7f80047
Show file tree
Hide file tree
Showing 38 changed files with 999 additions and 758 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pylm
.vscode
/shalm
bin
29 changes: 29 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Build the manager binary
FROM golang:1.13 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download

# Copy the go source
COPY main.go main.go
COPY api/ api/
COPY pkg/ pkg/
COPY cmd/ cmd/
COPY controllers/ controllers/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o shalm main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:nonroot
WORKDIR /
COPY --from=builder /workspace/shalm .
USER nonroot:nonroot

ENTRYPOINT ["/shalm","controller"]
80 changes: 80 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

# Image URL to use all building/pushing image targets
IMG ?= controller:latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true"

# 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

all: manager

# Run tests
test: generate fmt vet manifests
go test ./... -coverprofile cover.out

# Build manager binary
manager: generate fmt vet
go build -o bin/manager main.go

# Run against the configured Kubernetes cluster in ~/.kube/config
run: generate fmt vet manifests
go run ./main.go

# Install CRDs into a cluster
install: manifests
kustomize build config/crd | kubectl apply -f -

# Uninstall CRDs from a cluster
uninstall: manifests
kustomize build config/crd | kubectl delete -f -

# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
deploy: manifests
cd config/manager && kustomize edit set image controller=${IMG}
kustomize build config/default | kubectl apply -f -

# Generate manifests e.g. CRD, RBAC etc.
manifests: controller-gen
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases

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

# Run go vet against code
vet:
go vet ./...

# Generate code
generate: controller-gen
$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths="./..."

# Build the docker image
docker-build: test
docker build . -t ${IMG}

# Push the docker image
docker-push:
docker push ${IMG}

# find or download controller-gen
# download controller-gen if necessary
controller-gen:
ifeq (, $(shell which controller-gen))
@{ \
set -e ;\
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
cd $$CONTROLLER_GEN_TMP_DIR ;\
go mod init tmp ;\
go get sigs.k8s.io/controller-tools/cmd/[email protected] ;\
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
}
CONTROLLER_GEN=$(GOBIN)/controller-gen
else
CONTROLLER_GEN=$(shell which controller-gen)
endif
7 changes: 7 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
domain: kramerul.github.com
repo: github.com/kramerul/shalm
resources:
- group: shalm
kind: ShalmChart
version: v1alpha1
version: "2"
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ The following section describes the available methods inside `Chart.star`

### Chart

#### `chart("<url>",namespace=namespace,...)`
#### `chart("<url>",namespace=namespace,proxy=false,...)`

An new chart is created.
If no namespace is given, the namespace is inherited from the parent chart.
Expand All @@ -166,7 +166,8 @@ If no namespace is given, the namespace is inherited from the parent chart.
|-----------|-------------|
| url | The chart is loaded from the given url. The url can be relative. In this case the chart is loaded from a path relative to the current chart location. |
| namespace | If no namespace is given, the namespace is inherited from the parent chart. |
| ... | Additional parametes are passed to the `init` method of the corresponding chart. |
| proxy | If true, a proxy for the chart is returned. Applying or deleting a proxy chart is done using a `CustomerResource`. The installation process is performed by the `shalm-controller` |
| ... | Additional parameters are passed to the `init` method of the corresponding chart. |


#### `chart.apply(k8s)`
Expand Down Expand Up @@ -260,6 +261,17 @@ Wait for rollout status of one kubernetes object
| name | name of k8s object |
| timeout | Timeout passed to `kubectl apply`. A timeout of zero means wait forever. |

#### `k8s.wait(kind,name,condition, timeout=0)`

Wait for condition of one kubernetes object

| Parameter | Description |
|-----------|-------------|
| kind | k8s kind |
| name | name of k8s object |
| condition | condition |
| timeout | Timeout passed to `kubectl apply`. A timeout of zero means wait forever. |

### user_credential


Expand Down
35 changes: 35 additions & 0 deletions api/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
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.
*/

// Package v1alpha1 contains API Schema definitions for the shalm v1alpha1 API group
// +kubebuilder:object:generate=true
// +groupName=shalm.kramerul.github.com
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "kramerul.github.com", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
111 changes: 111 additions & 0 deletions api/v1alpha1/shalmchart_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
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.
*/

package v1alpha1

import (
"bytes"
"encoding/gob"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// ClonableMap -
type ClonableMap map[string]interface{}

// DeepCopy -
func (v *ClonableMap) DeepCopy() *ClonableMap {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
dec := gob.NewDecoder(&buf)
err := enc.Encode(v)
if err != nil {
panic(err)
}
var copy map[string]interface{}
err = dec.Decode(&copy)
if err != nil {
panic(err)
}
result := ClonableMap(copy)
return &result
}

// ClonableArray -
type ClonableArray []interface{}

// DeepCopy -
func (v *ClonableArray) DeepCopy() *ClonableArray {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
dec := gob.NewDecoder(&buf)
err := enc.Encode(v)
if err != nil {
panic(err)
}
var copy []interface{}
err = dec.Decode(&copy)
if err != nil {
panic(err)
}
result := ClonableArray(copy)
return &result
}

// ShalmChartSpec defines the desired state of ShalmChart
type ShalmChartSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

Values ClonableMap `json:"values,omitempty"`
Args ClonableArray `json:"args,omitempty"`
KwArgs ClonableMap `json:"kwargs,omitempty"`
KubeConfig string `json:"kubeconfig,omitempty"`
Namespace string `json:"namespace,omitempty"`
URL string `json:"url"`
}

// ShalmChartStatus defines the observed state of ShalmChart
type ShalmChartStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}

// +kubebuilder:object:root=true

// ShalmChart is the Schema for the shalmcharts API
type ShalmChart struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec ShalmChartSpec `json:"spec,omitempty"`
Status ShalmChartStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// ShalmChartList contains a list of ShalmChart
type ShalmChartList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []ShalmChart `json:"items"`
}

func init() {
SchemeBuilder.Register(&ShalmChart{}, &ShalmChartList{})
}
Loading

0 comments on commit 7f80047

Please sign in to comment.