Skip to content

Commit e28d30d

Browse files
authored
Merge pull request #60 from maleck13/fix-config
update config command to use configmap as per proposal
2 parents 1dc0545 + b1b5128 commit e28d30d

File tree

4 files changed

+54
-29
lines changed

4 files changed

+54
-29
lines changed

integration/client_get_config_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ func TestGetClientConfig(t *testing.T) {
4242

4343
output, err := cmd.CombinedOutput()
4444
if err != nil {
45-
t.Fatal(err)
45+
t.Fatal(err, string(output))
4646
}
4747

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

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

5555
if test.name == "json output" {

pkg/cmd/clientConfig.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/olekukonko/tablewriter"
2323
"github.com/pkg/errors"
2424
"github.com/spf13/cobra"
25+
"k8s.io/apimachinery/pkg/apis/meta/v1"
2526
"k8s.io/client-go/kubernetes"
2627
)
2728

@@ -61,23 +62,28 @@ kubectl plugin mobile get clientconfig`,
6162
return cmd.Usage()
6263
}
6364
clientID := args[0]
64-
if ns, err = currentNamespace(cmd.Flags()); err != nil {
65+
ns, err = currentNamespace(cmd.Flags())
66+
if err != nil {
6567
return err
6668
}
6769
ms := listServices(ns, ccc.k8Client)
6870
for _, svc := range ms {
6971
var svcConfig *ServiceConfig
7072
var err error
73+
configMap, err := ccc.k8Client.CoreV1().ConfigMaps(ns).Get(svc.Name, v1.GetOptions{})
74+
if err != nil {
75+
return errors.Wrap(err, "unable to create config. Failed to get service "+svc.Name+" configmap")
76+
}
7177
if _, ok := convertors[svc.Name]; !ok {
7278

7379
convertor := defaultSecretConvertor{}
74-
if svcConfig, err = convertor.Convert(svc); err != nil {
80+
if svcConfig, err = convertor.Convert(svc.ID, configMap.Data); err != nil {
7581
return err
7682
}
7783
} else {
7884
// we can only convert what is available
7985
convertor := convertors[svc.Name]
80-
if svcConfig, err = convertor.Convert(svc); err != nil {
86+
if svcConfig, err = convertor.Convert(svc.ID, configMap.Data); err != nil {
8187
return err
8288
}
8389
}

pkg/cmd/clientConfig_test.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ func TestClientConfigCmd_GetClientConfigCmd(t *testing.T) {
119119
},
120120
},
121121
Data: map[string][]byte{
122-
"name": []byte("keycloak"),
123-
"public_installation": []byte("{}"),
122+
"name": []byte("keycloak"),
124123
},
125124
},
126125
}
@@ -129,6 +128,26 @@ func TestClientConfigCmd_GetClientConfigCmd(t *testing.T) {
129128
}
130129
return true, secretList, nil
131130
})
131+
fakeclient.AddReactor("get", "configmaps", func(action ktesting.Action) (handled bool, ret runtime.Object, err error) {
132+
var config *v1.ConfigMap
133+
name := action.(ktesting.GetAction).GetName()
134+
if name == "keycloak" {
135+
config = &v1.ConfigMap{
136+
Data: map[string]string{
137+
"public_installation": "{}",
138+
"name": "keycloak",
139+
},
140+
}
141+
}
142+
if name == "test-service" {
143+
config = &v1.ConfigMap{
144+
Data: map[string]string{
145+
"name": "test-service",
146+
},
147+
}
148+
}
149+
return true, config, nil
150+
})
132151
return fakeclient
133152
},
134153
namespace: "testing-ns",

pkg/cmd/types.go

+22-22
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const (
7272

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

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

112112
//Convert a kubernetes secret to a mobile.ServiceConfig
113-
func (dsc defaultSecretConvertor) Convert(s *Service) (*ServiceConfig, error) {
113+
func (dsc defaultSecretConvertor) Convert(id string, params map[string]string) (*ServiceConfig, error) {
114114
config := map[string]interface{}{}
115115
headers := map[string]string{}
116-
for k, v := range s.Params {
116+
for k, v := range params {
117117
if !defaultIgnored.Contains(k) {
118118
config[k] = string(v)
119119
}
@@ -122,21 +122,21 @@ func (dsc defaultSecretConvertor) Convert(s *Service) (*ServiceConfig, error) {
122122
config["headers"] = headers
123123
}
124124
return &ServiceConfig{
125-
ID: string(s.ID),
126-
Name: string(s.Name),
127-
URL: string(s.Host),
128-
Type: string(s.Type),
125+
ID: id,
126+
Name: params["name"],
127+
URL: params["uri"],
128+
Type: params["type"],
129129
Config: config,
130130
}, nil
131131
}
132132

133133
type keycloakSecretConvertor struct{}
134134

135135
//Convert a kubernetes keycloak secret into a keycloak mobile.ServiceConfig
136-
func (ksc keycloakSecretConvertor) Convert(s *Service) (*ServiceConfig, error) {
136+
func (ksc keycloakSecretConvertor) Convert(id string, params map[string]string) (*ServiceConfig, error) {
137137
config := map[string]interface{}{}
138138
headers := map[string]string{}
139-
err := json.Unmarshal([]byte(s.Params["public_installation"]), &config)
139+
err := json.Unmarshal([]byte(params["public_installation"]), &config)
140140
if err != nil {
141141
return nil, errors.Wrap(err, "failed to unmarshall keycloak configuration ")
142142
}
@@ -145,25 +145,25 @@ func (ksc keycloakSecretConvertor) Convert(s *Service) (*ServiceConfig, error) {
145145
}
146146
return &ServiceConfig{
147147
Config: config,
148-
Name: string(s.Name),
149-
URL: string(s.Host),
150-
Type: string(s.Type),
151-
ID: string(s.ID),
148+
ID: id,
149+
Name: string(params["name"]),
150+
URL: string(params["uri"]),
151+
Type: string(params["type"]),
152152
}, nil
153153
}
154154

155155
type syncSecretConvertor struct{}
156156

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

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

176176
return &ServiceConfig{
177177
Config: config,
178-
Name: string(s.Name),
179-
URL: string(s.Host),
180-
Type: string(s.Type),
181-
ID: string(s.ID),
178+
ID: id,
179+
Name: params["name"],
180+
URL: params["uri"],
181+
Type: params["type"],
182182
}, nil
183183
}
184184

0 commit comments

Comments
 (0)