Skip to content

Commit

Permalink
Merge pull request #42 from replicatedhq/admin-console-branding
Browse files Browse the repository at this point in the history
Admin console branding
  • Loading branch information
marccampbell authored Sep 8, 2019
2 parents e46e719 + a5378ba commit f88ed59
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 4 deletions.
79 changes: 79 additions & 0 deletions ffi/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import "C"

import (
"fmt"

"github.com/replicatedhq/kots/pkg/upstream"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)

//export ReadMetadata
func ReadMetadata(namespace string) *C.char {
cfg, err := config.GetConfig()
if err != nil {
fmt.Printf("error getting kubernetes config: %s\n", err.Error())
return C.CString(upstream.DefaultMetadata)
}

clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
fmt.Printf("error creating kubernetes clientset: %s\n", err.Error())
return C.CString(upstream.DefaultMetadata)
}

configMap, err := clientset.CoreV1().ConfigMaps(namespace).Get("kotsadm-application-metadata", metav1.GetOptions{})
if err != nil {
if kuberneteserrors.IsNotFound(err) {
return C.CString(upstream.DefaultMetadata)
}

fmt.Printf("error reading branding: %s\n", err.Error())
return C.CString(upstream.DefaultMetadata)
}

data, ok := configMap.Data["application.yaml"]
if !ok {
fmt.Printf("metdata did not contain required key: %#v\n", configMap.Data)
return C.CString(upstream.DefaultMetadata)
}

return C.CString(data)
}

//export RemoveMetadata
func RemoveMetadata(namespace string) int {
cfg, err := config.GetConfig()
if err != nil {
fmt.Printf("error getting kubernetes config: %s\n", err.Error())
return -1
}

clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
fmt.Printf("error creating kubernetes clientset: %s\n", err.Error())
return -1
}

_, err = clientset.CoreV1().ConfigMaps(namespace).Get("kotsadm-application-metadata", metav1.GetOptions{})
if err != nil {
if kuberneteserrors.IsNotFound(err) {
return 0
}

fmt.Printf("error reading branding: %s\n", err.Error())
return -1
}

err = clientset.CoreV1().ConfigMaps(namespace).Delete("kotsadm-application-metadata", &metav1.DeleteOptions{})
if err != nil {
fmt.Printf("error deleting metadata: %s\n", err.Error())
return -1
}

return 0
}
3 changes: 3 additions & 0 deletions pkg/kotsadm/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func waitForAPI(deployOptions *DeployOptions, clientset *kubernetes.Clientset) e
}

func ensureAPI(deployOptions *DeployOptions, clientset *kubernetes.Clientset) error {
if err := ensureApplicationMetadata(*deployOptions, clientset); err != nil {
return errors.Wrap(err, "failed to ensure custom branding")
}
if err := ensureAPIDeployment(*deployOptions, clientset); err != nil {
return errors.Wrap(err, "failed to ensure api deployment")
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/kotsadm/application_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"bytes"

"github.com/pkg/errors"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
)

Expand All @@ -20,3 +23,19 @@ func getApplicationMetadataYAML(data []byte, namespace string) (map[string][]byt

return docs, nil
}

func ensureApplicationMetadata(deployOptions DeployOptions, clientset *kubernetes.Clientset) error {
_, err := clientset.CoreV1().ConfigMaps(deployOptions.Namespace).Get("kotsadm-application-metadata", metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to get existing metadata config map")
}

_, err := clientset.CoreV1().ConfigMaps(deployOptions.Namespace).Create(applicationMetadataConfig(deployOptions.ApplicationMetadata, deployOptions.Namespace))
if err != nil {
return errors.Wrap(err, "failed to create metadata config map")
}
}

return nil
}
1 change: 0 additions & 1 deletion pkg/kotsadm/kotsadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ func Deploy(deployOptions DeployOptions) error {
return errors.Wrap(err, "failed to create namespace")
}
log.FinishChildSpinner()

} else if err != nil {
return errors.Wrap(err, "failed to get namespace")
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/upstream/replicated.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ import (
"k8s.io/client-go/kubernetes/scheme"
)

const defaultMetadata = `apiVersion: kots.io/v1beta1
const DefaultMetadata = `apiVersion: kots.io/v1beta1
kind: Application
metadata:
name: "Application"
spec:
name: "Application"
title: "Application"
icon: https://cdn1.iconfinder.com/data/icons/ninja-things-1/1772/ninja-simple-512.png`

type ReplicatedUpstream struct {
Expand Down Expand Up @@ -480,7 +480,7 @@ func GetApplicationMetadata(upstream *url.URL) ([]byte, error) {
}

if metadata == nil {
metadata = []byte(defaultMetadata)
metadata = []byte(DefaultMetadata)
}

return metadata, nil
Expand Down

0 comments on commit f88ed59

Please sign in to comment.