Skip to content

Commit

Permalink
Merge pull request #60 from maleck13/fix-config
Browse files Browse the repository at this point in the history
update config command to use configmap as per proposal
  • Loading branch information
maleck13 authored Feb 1, 2018
2 parents 1dc0545 + b1b5128 commit e28d30d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 29 deletions.
4 changes: 2 additions & 2 deletions integration/client_get_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ func TestGetClientConfig(t *testing.T) {

output, err := cmd.CombinedOutput()
if err != nil {
t.Fatal(err)
t.Fatal(err, string(output))
}

actual := string(output)
expected := LoadSnapshot(t, getClientTestPath+test.fixture)

if *update {
WriteSnapshot(t, getClientTestPath+test.fixture, output)
WriteSnapshot(t, getClientTestPath+test.fixture, []byte(CleanStringByRegex(actual, regexes)))
}

if test.name == "json output" {
Expand Down
12 changes: 9 additions & 3 deletions pkg/cmd/clientConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

Expand Down Expand Up @@ -61,23 +62,28 @@ kubectl plugin mobile get clientconfig`,
return cmd.Usage()
}
clientID := args[0]
if ns, err = currentNamespace(cmd.Flags()); err != nil {
ns, err = currentNamespace(cmd.Flags())
if err != nil {
return err
}
ms := listServices(ns, ccc.k8Client)
for _, svc := range ms {
var svcConfig *ServiceConfig
var err error
configMap, err := ccc.k8Client.CoreV1().ConfigMaps(ns).Get(svc.Name, v1.GetOptions{})
if err != nil {
return errors.Wrap(err, "unable to create config. Failed to get service "+svc.Name+" configmap")
}
if _, ok := convertors[svc.Name]; !ok {

convertor := defaultSecretConvertor{}
if svcConfig, err = convertor.Convert(svc); err != nil {
if svcConfig, err = convertor.Convert(svc.ID, configMap.Data); err != nil {
return err
}
} else {
// we can only convert what is available
convertor := convertors[svc.Name]
if svcConfig, err = convertor.Convert(svc); err != nil {
if svcConfig, err = convertor.Convert(svc.ID, configMap.Data); err != nil {
return err
}
}
Expand Down
23 changes: 21 additions & 2 deletions pkg/cmd/clientConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ func TestClientConfigCmd_GetClientConfigCmd(t *testing.T) {
},
},
Data: map[string][]byte{
"name": []byte("keycloak"),
"public_installation": []byte("{}"),
"name": []byte("keycloak"),
},
},
}
Expand All @@ -129,6 +128,26 @@ func TestClientConfigCmd_GetClientConfigCmd(t *testing.T) {
}
return true, secretList, nil
})
fakeclient.AddReactor("get", "configmaps", func(action ktesting.Action) (handled bool, ret runtime.Object, err error) {
var config *v1.ConfigMap
name := action.(ktesting.GetAction).GetName()
if name == "keycloak" {
config = &v1.ConfigMap{
Data: map[string]string{
"public_installation": "{}",
"name": "keycloak",
},
}
}
if name == "test-service" {
config = &v1.ConfigMap{
Data: map[string]string{
"name": "test-service",
},
}
}
return true, config, nil
})
return fakeclient
},
namespace: "testing-ns",
Expand Down
44 changes: 22 additions & 22 deletions pkg/cmd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const (

// SecretConvertor converts a kubernetes secret into a mobile.ServiceConfig
type SecretConvertor interface {
Convert(s *Service) (*ServiceConfig, error)
Convert(id string, params map[string]string) (*ServiceConfig, error)
}

//ServiceConfigs are collection of configurations for services in a specific namespace
Expand Down Expand Up @@ -110,10 +110,10 @@ func (i ignoredFields) Contains(field string) bool {
var defaultIgnored = ignoredFields{"password", "token", "url", "uri", "name", "type", "id"}

//Convert a kubernetes secret to a mobile.ServiceConfig
func (dsc defaultSecretConvertor) Convert(s *Service) (*ServiceConfig, error) {
func (dsc defaultSecretConvertor) Convert(id string, params map[string]string) (*ServiceConfig, error) {
config := map[string]interface{}{}
headers := map[string]string{}
for k, v := range s.Params {
for k, v := range params {
if !defaultIgnored.Contains(k) {
config[k] = string(v)
}
Expand All @@ -122,21 +122,21 @@ func (dsc defaultSecretConvertor) Convert(s *Service) (*ServiceConfig, error) {
config["headers"] = headers
}
return &ServiceConfig{
ID: string(s.ID),
Name: string(s.Name),
URL: string(s.Host),
Type: string(s.Type),
ID: id,
Name: params["name"],
URL: params["uri"],
Type: params["type"],
Config: config,
}, nil
}

type keycloakSecretConvertor struct{}

//Convert a kubernetes keycloak secret into a keycloak mobile.ServiceConfig
func (ksc keycloakSecretConvertor) Convert(s *Service) (*ServiceConfig, error) {
func (ksc keycloakSecretConvertor) Convert(id string, params map[string]string) (*ServiceConfig, error) {
config := map[string]interface{}{}
headers := map[string]string{}
err := json.Unmarshal([]byte(s.Params["public_installation"]), &config)
err := json.Unmarshal([]byte(params["public_installation"]), &config)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshall keycloak configuration ")
}
Expand All @@ -145,25 +145,25 @@ func (ksc keycloakSecretConvertor) Convert(s *Service) (*ServiceConfig, error) {
}
return &ServiceConfig{
Config: config,
Name: string(s.Name),
URL: string(s.Host),
Type: string(s.Type),
ID: string(s.ID),
ID: id,
Name: string(params["name"]),
URL: string(params["uri"]),
Type: string(params["type"]),
}, nil
}

type syncSecretConvertor struct{}

//Convert a kubernetes Sync Server secret into a keycloak mobile.ServiceConfig
func (scc syncSecretConvertor) Convert(s *Service) (*ServiceConfig, error) {
func (scc syncSecretConvertor) Convert(id string, params map[string]string) (*ServiceConfig, error) {
config := map[string]interface{}{
"url": s.Host,
"url": params["host"],
}
headers := map[string]string{}

acAppID, acAppIDExists := s.Params["apicast_app_id"]
acAppKey, acAppKeyExists := s.Params["apicast_app_key"]
acRoute, acRouteExists := s.Params["apicast_route"]
acAppID, acAppIDExists := params["apicast_app_id"]
acAppKey, acAppKeyExists := params["apicast_app_key"]
acRoute, acRouteExists := params["apicast_route"]
if acAppIDExists && acAppKeyExists && acRouteExists {
headers["app_id"] = acAppID
headers["app_key"] = acAppKey
Expand All @@ -175,10 +175,10 @@ func (scc syncSecretConvertor) Convert(s *Service) (*ServiceConfig, error) {

return &ServiceConfig{
Config: config,
Name: string(s.Name),
URL: string(s.Host),
Type: string(s.Type),
ID: string(s.ID),
ID: id,
Name: params["name"],
URL: params["uri"],
Type: params["type"],
}, nil
}

Expand Down

0 comments on commit e28d30d

Please sign in to comment.