Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#2756] Apply config through the airy controller #3176

Merged
merged 13 commits into from
May 31, 2022
1 change: 1 addition & 0 deletions cli/pkg/cmd/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_library(
"//cli/pkg/cmd/upgrade",
"//cli/pkg/kube",
"//cli/pkg/workspace",
"//lib/go/k8s",
"@com_github_spf13_cobra//:cobra",
"@com_github_spf13_viper//:viper",
],
Expand Down
1 change: 1 addition & 0 deletions cli/pkg/cmd/api/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//cli/pkg/kube",
"//lib/go/k8s",
"@com_github_spf13_cobra//:cobra",
"@com_github_spf13_viper//:viper",
],
Expand Down
3 changes: 2 additions & 1 deletion cli/pkg/cmd/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"

"github.com/airyhq/airy/lib/go/k8s"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -24,7 +25,7 @@ func endpoint(cmd *cobra.Command, args []string) {
os.Exit(1)
}

coreConfig, err := kube.GetCmData("core-config", viper.GetString("namespace"), set)
coreConfig, err := k8s.GetCmData("core-config", viper.GetString("namespace"), set)
if err != nil {
fmt.Println("could not find an installation of Airy Core. Get started here https://airy.co/docs/core/getting-started/installation/introduction")
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions cli/pkg/cmd/config/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ go_library(
"//cli/pkg/console",
"//cli/pkg/kube",
"//cli/pkg/workspace",
"//lib/go/kubectl/configmaps",
"//lib/go/httpclient",
"//lib/go/k8s",
"@com_github_spf13_cobra//:cobra",
"@com_github_spf13_viper//:viper",
"@in_gopkg_yaml_v2//:yaml_v2",
"@io_k8s_apimachinery//pkg/apis/meta/v1:go_default_library",
],
)

Expand Down
95 changes: 19 additions & 76 deletions cli/pkg/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package config

import (
"cli/pkg/console"
"cli/pkg/kube"
"cli/pkg/workspace"
"context"
"fmt"
"os"
"text/tabwriter"

"github.com/airyhq/airy/lib/go/kubectl/configmaps"
"github.com/airyhq/airy/lib/go/httpclient"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var configFile string
Expand All @@ -26,22 +24,15 @@ var ConfigCmd = &cobra.Command{
}

func getConfig(cmd *cobra.Command, args []string) {
namespace := viper.GetString("namespace")
kubeCtx := kube.Load()
clientSet, err := kubeCtx.GetClientSet()
if err != nil {
fmt.Printf(err.Error())
console.Exit("could not find an installation of Airy Core. Get started here https://airy.co/docs/core/getting-started/installation/introduction")
}
systemToken := viper.GetString("systemToken")
c := httpclient.NewClient(viper.GetString("apihost"))
c.Token = systemToken

identity := func(d map[string]string) map[string]string { return d }

components, err := configmaps.GetComponentsConfigMaps(context.Background(), namespace, clientSet, identity)
res, err := c.ComponentsGet()
if err != nil {
console.Exit(err.Error())
console.Exit("could not get config: ", err)
}

blob, err := yaml.Marshal(map[string]interface{}{"components": components})
blob, err := yaml.Marshal(map[string]interface{}{"components": res})
if err != nil {
console.Exit("could not marshal components list %s", err)
}
Expand All @@ -63,78 +54,30 @@ func ApplyConfig(workspacePath string) {
fmt.Println(err.Error())
os.Exit(1)
}
namespace := viper.GetString("namespace")
systemToken := viper.GetString("systemToken")
conf, err := dir.LoadAiryYaml()
if err != nil {
console.Exit("error parsing configuration file: ", err)
}
kubeCtx := kube.Load()
clientset, err := kubeCtx.GetClientSet()
if err != nil {
console.Exit("could not find an installation of Airy Core. Get started here https://airy.co/docs/core/getting-started/installation/introduction")
}
c := httpclient.NewClient(viper.GetString("apihost"))

secData := getSecurityData(conf.Security)
if len(secData) != 0 {
applyErr := kube.ApplyConfigMap("security", namespace, secData, map[string]string{}, clientset)
if applyErr != nil {
// TODO should we error here?
fmt.Printf("unable to apply configuration for \"security\"\n Error:\n %v\n", applyErr)
} else {
fmt.Printf("applied configuration for \"security\"\n")
}
}
c.Token = systemToken

configuredComponents := make(map[string]bool)
for componentType, _ := range conf.Components {
for componentName, componentValues := range conf.Components[componentType] {
configmapName := componentType + "-" + componentName
labels := map[string]string{
"core.airy.co/component": configmapName,
}
applyErr := kube.ApplyConfigMap(configmapName, namespace, componentValues, labels, clientset)
configuredComponents[configmapName] = true
if applyErr != nil {
fmt.Printf("unable to apply configuration for component: \"%s-%s\"\n Error:\n %v\n", componentType, componentName, applyErr)
} else {
fmt.Printf("applied configuration for component: \"%s-%s\"\n", componentType, componentName)
}
}
}
res, err := c.ComponentsUpdate(conf)

configmapList, _ := clientset.CoreV1().ConfigMaps(namespace).List(context.TODO(), v1.ListOptions{LabelSelector: "core.airy.co/component"})
for _, configmap := range configmapList.Items {
if !configuredComponents[configmap.ObjectMeta.Name] {
deleteErr := kube.DeleteConfigMap(configmap.ObjectMeta.Name, namespace, clientset)
if deleteErr != nil {
fmt.Printf("unable to remove configuration for component %s.\n", configmap.ObjectMeta.Name)
} else {
fmt.Printf("removed configuration for component \"%s\".\n", configmap.ObjectMeta.Name)
}
}
if err != nil {
console.Exit("could not apply config: ", err)
}
}

func getSecurityData(s workspace.SecurityConf) map[string]string {
m := make(map[string]string, len(s.Oidc))
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
fmt.Fprintf(w, "Configured component\t\tEnabled\n")

if s.SystemToken != "" {
m["systemToken"] = s.SystemToken
}
if s.AllowedOrigins != "" {
m["allowedOrigins"] = s.AllowedOrigins
}
if s.JwtSecret != "" {
m["jwtSecret"] = s.JwtSecret
for _, component := range res {
fmt.Fprintf(w, "%s\t\t%t\n", component.Name, component.Enabled)
}

for key, value := range s.Oidc {
if value != "" {
m["oidc."+key] = value
}
}
w.Flush()

return m
}

var applyConfigCmd = &cobra.Command{
Expand Down
2 changes: 1 addition & 1 deletion cli/pkg/cmd/create/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ go_library(
"//cli/pkg/cmd/config",
"//cli/pkg/console",
"//cli/pkg/helm",
"//cli/pkg/kube",
"//cli/pkg/providers",
"//cli/pkg/workspace",
"//lib/go/k8s",
"@com_github_spf13_cobra//:cobra",
"@com_github_spf13_viper//:viper",
"@com_github_twinproduction_go_color//:go-color",
Expand Down
5 changes: 3 additions & 2 deletions cli/pkg/cmd/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import (
"cli/pkg/cmd/config"
"cli/pkg/console"
"cli/pkg/helm"
"cli/pkg/kube"
"cli/pkg/providers"
"cli/pkg/workspace"
"fmt"
"os"
"runtime"

"github.com/airyhq/airy/lib/go/k8s"

"github.com/TwinProduction/go-color"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -114,7 +115,7 @@ func create(cmd *cobra.Command, args []string) {

fmt.Println("🎉 Your Airy Core is ready")

coreConfig, err := kube.GetCmData("core-config", namespace, clientset)
coreConfig, err := k8s.GetCmData("core-config", namespace, clientset)
if err != nil {
console.Exit("failed to get hosts from installation")
}
Expand Down
3 changes: 2 additions & 1 deletion cli/pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"strings"
"time"

"github.com/airyhq/airy/lib/go/k8s"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -93,7 +94,7 @@ var versionCmd = &cobra.Command{
return
}

coreConfig, err := kube.GetCmData("core-config", viper.GetString("namespace"), set)
coreConfig, err := k8s.GetCmData("core-config", viper.GetString("namespace"), set)
if err != nil {
fmt.Println("Unable to retrieve the kubernetes config map:", err.Error())
} else if airyVersion, ok := coreConfig["APP_IMAGE_TAG"]; ok {
Expand Down
1 change: 1 addition & 0 deletions cli/pkg/cmd/ui/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//cli/pkg/kube",
"//lib/go/k8s",
"@com_github_spf13_cobra//:cobra",
"@com_github_spf13_viper//:viper",
],
Expand Down
4 changes: 3 additions & 1 deletion cli/pkg/cmd/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/spf13/viper"

"github.com/spf13/cobra"

"github.com/airyhq/airy/lib/go/k8s"
)

// UICmd opens the Airy Core UI
Expand All @@ -29,7 +31,7 @@ func ui(cmd *cobra.Command, args []string) {
os.Exit(1)
}

coreConfig, err := kube.GetCmData("core-config", viper.GetString("namespace"), set)
coreConfig, err := k8s.GetCmData("core-config", viper.GetString("namespace"), set)
if err != nil {
fmt.Println("could not find an installation of Airy Core. Get started here https://airy.co/docs/core/getting-started/installation/introduction")
os.Exit(1)
Expand Down
1 change: 1 addition & 0 deletions cli/pkg/cmd/upgrade/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ go_library(
"//cli/pkg/kube",
"//cli/pkg/workspace",
"//infrastructure/lib/go/k8s/util",
"//lib/go/k8s",
"@com_github_spf13_cobra//:cobra",
"@com_github_spf13_viper//:viper",
],
Expand Down
3 changes: 2 additions & 1 deletion cli/pkg/cmd/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"

"github.com/airyhq/airy/infrastructure/lib/go/k8s/util"
"github.com/airyhq/airy/lib/go/k8s"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -54,7 +55,7 @@ func upgrade(cmd *cobra.Command, args []string) {
if version == "" {
version = Version
}
clusterAiryCore, err := kube.GetCmData("core-config", namespace, clientset)
clusterAiryCore, err := k8s.GetCmData("core-config", namespace, clientset)
if err != nil {
console.Exit("Unable to retrieve existing version of Airy Core: ", err.Error())
}
Expand Down
7 changes: 1 addition & 6 deletions cli/pkg/kube/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "kube",
srcs = [
"configmaps.go",
"context.go",
],
srcs = ["context.go"],
importpath = "cli/pkg/kube",
visibility = ["//visibility:public"],
deps = [
"@com_github_spf13_viper//:viper",
"@io_k8s_api//core/v1:go_default_library",
"@io_k8s_apimachinery//pkg/apis/meta/v1:go_default_library",
"@io_k8s_client_go//kubernetes:go_default_library",
"@io_k8s_client_go//plugin/pkg/client/auth/gcp:go_default_library",
"@io_k8s_client_go//tools/clientcmd:go_default_library",
Expand Down
2 changes: 1 addition & 1 deletion cli/pkg/workspace/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "workspace",
srcs = [
"airy_yaml.go",
"files.go",
"init.go",
],
importpath = "cli/pkg/workspace",
visibility = ["//visibility:public"],
deps = [
"//cli/pkg/workspace/template",
"//lib/go/config",
"@com_github_spf13_viper//:viper",
"@in_gopkg_yaml_v2//:yaml_v2",
],
Expand Down
13 changes: 8 additions & 5 deletions cli/pkg/workspace/files.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package workspace

import (
"gopkg.in/yaml.v2"
"io/ioutil"
"path/filepath"

"github.com/airyhq/airy/lib/go/config"

"gopkg.in/yaml.v2"
)

const cliConfigFileName = "cli.yaml"
Expand All @@ -16,17 +19,17 @@ func (f ConfigDir) GetAiryYaml() string {
return filepath.Join(f.Path, "airy.yaml")
}

func (f ConfigDir) LoadAiryYaml() (AiryConf, error) {
func (f ConfigDir) LoadAiryYaml() (config.AiryConf, error) {
data, err := ioutil.ReadFile(f.GetAiryYaml())
if err != nil {
return AiryConf{}, err
return config.AiryConf{}, err
}
conf := AiryConf{}
conf := config.AiryConf{}
err = yaml.Unmarshal(data, &conf)
return conf, err
}

func (f ConfigDir) UpdateAiryYaml(apply func(AiryConf) AiryConf) error {
func (f ConfigDir) UpdateAiryYaml(apply func(config.AiryConf) config.AiryConf) error {
airyYaml, err := f.LoadAiryYaml()
if err != nil {
return err
Expand Down
Loading