Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
feat: allow custom-resource-lookup with kget
Browse files Browse the repository at this point in the history
Signed-off-by: Tarun Khandelwal <tarun@flanksource.com>
  • Loading branch information
Kaitou786 committed May 13, 2021
1 parent d09cbe1 commit 0c3b97c
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
go-version: [1.13.x]
platform: [ubuntu-latest]
k8s:
- v1.18.6
- v1.20.0
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.13

require (
github.com/flanksource/commons v1.5.1
github.com/flanksource/kommons v0.7.2
github.com/flanksource/kommons v0.13.0
github.com/go-logr/logr v0.3.0
github.com/go-logr/zapr v0.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/flanksource/commons v1.5.1 h1:yvOQYQ7PwZ+FasCpGR4ky4tmfi0AP8hN9d1nd6mgBXo=
github.com/flanksource/commons v1.5.1/go.mod h1:v76CUuAsGNgF6MYxQ1XJNwQ4N+WSJbsQ+fTILbP4YP0=
github.com/flanksource/kommons v0.7.2 h1:U0gLG1fRqwRk1NogVZnZoaJgWr6QTq9Ax5t7wZpLALc=
github.com/flanksource/kommons v0.7.2/go.mod h1:7tDF0fPqJeRB1dAUI71Mxmdhd0MYaF3YYHdp4XPgIzM=
github.com/flanksource/kommons v0.13.0 h1:8kvfnTzJLPX5AlK97W5pI8qvp6HPl/V1PiR5b2L5xs0=
github.com/flanksource/kommons v0.13.0/go.mod h1:7tDF0fPqJeRB1dAUI71Mxmdhd0MYaF3YYHdp4XPgIzM=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BHsljHzVlRcyQhjrss6TZTdY2VfCqZPbv5k3iBFa2ZQ=
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
github.com/form3tech-oss/jwt-go v3.2.2+incompatible h1:TcekIExNqud5crz4xD2pavyTgWiPvpYe4Xau31I0PRk=
Expand Down
34 changes: 32 additions & 2 deletions k8s/patches.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ package k8s
import (
"bytes"
"fmt"
"github.com/flanksource/commons/logger"
"github.com/flanksource/kommons"
"io/ioutil"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/homedir"
"os"
"path"
"path/filepath"
osruntime "runtime"
"strings"
Expand All @@ -15,7 +22,6 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/kustomize"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/gvk"
"sigs.k8s.io/kustomize/pkg/patch"
Expand Down Expand Up @@ -44,7 +50,7 @@ func NewPatchApplier(clientset *kubernetes.Clientset, schemaManager *SchemaManag
SchemaManager: schemaManager,
}

functions := ktemplate.NewFunctions(clientset)
functions := ktemplate.NewFunctions(KommonsClient())
p.FuncMap = functions.FuncMap()
return p, nil
}
Expand Down Expand Up @@ -186,3 +192,27 @@ func stripAnnotations(obj *unstructured.Unstructured) {
}
obj.SetAnnotations(annotations)
}

func KommonsClient() *kommons.Client {
byts, err := GetKubeConfig()
if err != nil {
logger.Fatalf("failed to get kubeconfig: %v", err)
}
client, err := kommons.NewClientFromBytes(byts)
if err != nil {
logger.Fatalf("failed to create kommons.Client: %v", err)
}
return client
}

func GetKubeConfig() ([]byte, error) {
if env := os.Getenv("KUBECONFIG"); env != "" {
return ioutil.ReadFile(env)
}

if home := homedir.HomeDir(); home != "" {
return ioutil.ReadFile(path.Join(home, ".kube", "config"))
}

return nil, errors.Errorf("failed to find kube config")
}
33 changes: 2 additions & 31 deletions k8s/schema_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ package k8s_test

import (
"fmt"
"io/ioutil"
"os"
"path"
"reflect"
"strings"

"github.com/flanksource/commons/logger"
"github.com/flanksource/kommons"
"github.com/flanksource/template-operator/k8s"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -18,7 +14,6 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/homedir"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -412,7 +407,7 @@ func newSchemaManager() *k8s.SchemaManager {
}

func clientset() *kubernetes.Clientset {
client := kommonsClient()
client := k8s.KommonsClient()
clientset, err := client.GetClientset()
if err != nil {
logger.Fatalf("failed to get clientset: %v", err)
Expand All @@ -421,7 +416,7 @@ func clientset() *kubernetes.Clientset {
}

func crdClient() extapi.ApiextensionsV1beta1Interface {
client := kommonsClient()
client := k8s.KommonsClient()
restConfig, err := client.GetRESTConfig()
if err != nil {
logger.Fatalf("failed to get rest config: %v", err)
Expand All @@ -432,27 +427,3 @@ func crdClient() extapi.ApiextensionsV1beta1Interface {
}
return crdClient
}

func kommonsClient() *kommons.Client {
bytes, err := getKubeConfig()
if err != nil {
logger.Fatalf("failed to get kubeconfig: %v", err)
}
client, err := kommons.NewClientFromBytes(bytes)
if err != nil {
logger.Fatalf("failed to create kommons.Client: %v", err)
}
return client
}

func getKubeConfig() ([]byte, error) {
if env := os.Getenv("KUBECONFIG"); env != "" {
return ioutil.ReadFile(env)
}

if home := homedir.HomeDir(); home != "" {
return ioutil.ReadFile(path.Join(home, ".kube", "config"))
}

return nil, errors.Errorf("failed to find kube config")
}
2 changes: 1 addition & 1 deletion k8s/template_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func NewTemplateManager(c *kommons.Client, log logr.Logger, cache *SchemaCache,
return nil, errors.Wrap(err, "faile to create patch applier")
}

functions := ktemplate.NewFunctions(clientset)
functions := ktemplate.NewFunctions(c)

tm := &TemplateManager{
Client: c,
Expand Down
2 changes: 1 addition & 1 deletion k8s/template_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ spec:

eventsRecorder := &TestEventRecorder{}
cache := k8s.NewSchemaCache(clientset(), 5*time.Minute, testLog)
templateManager, err := k8s.NewTemplateManager(kommonsClient(), testLog, cache, eventsRecorder)
templateManager, err := k8s.NewTemplateManager(k8s.KommonsClient(), testLog, cache, eventsRecorder)
Expect(err).ToNot(HaveOccurred())

result, err := templateManager.Template([]byte(templateJSON), db)
Expand Down
70 changes: 70 additions & 0 deletions test/fixtures/custom-resource-output-lookup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: samples.apps.flanksource.com
spec:
group: apps.flanksource.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
key:
type: string
status:
type: object
properties:
conditions:
type: array
items:
type: object
properties:
type:
type: string
status:
type: string
scope: Namespaced
names:
plural: samples
singular: sample
kind: Sample
---

apiVersion: templating.flanksource.com/v1
kind: Template
metadata:
name: resource-output-example
spec:
source:
apiVersion: apps.flanksource.com/v1
kind: Sample
resources:
- apiVersion: apps/v1
kind: Deployment
metadata:
name: "{{.metadata.name}}"
namespace: "{{.metadata.namespace}}"
labels:
app: '{{kget "Sample/test/test-sample" "spec.key"}}'
spec:
replicas: "{{.spec.replicas | default 1}}"
selector:
matchLabels:
app: '{{kget "Sample/test/test-sample" "spec.key"}}'
template:
metadata:
labels:
app: '{{kget "Sample/sample-ns/test-sample" "spec.key"}}'
spec:
containers:
- name: 'nginx'
image: "nginx"
ports:
- containerPort: 80

0 comments on commit 0c3b97c

Please sign in to comment.