generated from giantswarm/template-operator
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
respect capv wc proxy enabled setting (#530)
Co-authored-by: Xavier <[email protected]>
- Loading branch information
1 parent
b8272f7
commit 4479dd0
Showing
4 changed files
with
261 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package clustersecret | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
capi "sigs.k8s.io/cluster-api/api/v1beta1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/yaml" | ||
|
||
appv1alpha1 "github.com/giantswarm/apiextensions-application/api/v1alpha1" | ||
"github.com/giantswarm/microerror" | ||
) | ||
|
||
func vsphereProxyEnabled(ctx context.Context, ctrlClient client.Client, cluster capi.Cluster) (bool, error) { | ||
var userConfig string | ||
{ | ||
var clusterApp appv1alpha1.App | ||
|
||
err := ctrlClient.Get(ctx, client.ObjectKey{Namespace: cluster.GetNamespace(), Name: cluster.GetName()}, &clusterApp) | ||
if err != nil { | ||
return false, microerror.Mask(err) | ||
} | ||
|
||
if clusterApp.Spec.Name != "cluster-vsphere" { | ||
return false, nil | ||
} | ||
userConfig = clusterApp.Spec.UserConfig.ConfigMap.Name | ||
} | ||
|
||
if userConfig == "" { | ||
return false, nil | ||
} | ||
|
||
var configMap corev1.ConfigMap | ||
{ | ||
err := ctrlClient.Get(ctx, client.ObjectKey{Namespace: cluster.GetNamespace(), Name: userConfig}, &configMap) | ||
if err != nil { | ||
return false, microerror.Mask(err) | ||
} | ||
} | ||
|
||
return getProxyEnabledValueFromConfigMap(configMap) | ||
} | ||
|
||
func getProxyEnabledValueFromConfigMap(configMap corev1.ConfigMap) (bool, error) { | ||
|
||
var values map[string]interface{} | ||
{ | ||
if configMap.Data == nil { | ||
return false, nil | ||
} | ||
if configMap.Data["values"] == "" { | ||
return false, nil | ||
} | ||
data := configMap.Data["values"] | ||
data = strings.TrimPrefix(data, "|") | ||
data = strings.TrimPrefix(data, "\n") | ||
|
||
err := yaml.Unmarshal([]byte(data), &values) | ||
if err != nil { | ||
return false, microerror.Mask(err) | ||
} | ||
} | ||
|
||
// check for global.connectivity.proxy.enabled key | ||
if value, ok := values["global"].(map[string]interface{}); ok { | ||
if value, ok := value["connectivity"].(map[string]interface{}); ok { | ||
if value, ok := value["proxy"].(map[string]interface{}); ok { | ||
if value, ok := value["enabled"].(bool); ok { | ||
return value, nil | ||
} | ||
} | ||
} | ||
} | ||
|
||
return false, nil | ||
} |
171 changes: 171 additions & 0 deletions
171
service/controller/resource/clustersecret/vsphere_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package clustersecret | ||
|
||
import ( | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func Test_GetProxyEnabledValueFromConfigMap(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
configMap corev1.ConfigMap | ||
result bool | ||
expectError bool | ||
}{ | ||
{ | ||
name: "case 0", | ||
configMap: corev1.ConfigMap{ | ||
Data: map[string]string{ | ||
"values": getValuesProxyEnabled(), | ||
}, | ||
}, | ||
result: true, | ||
}, | ||
{ | ||
name: "case 1", | ||
configMap: corev1.ConfigMap{ | ||
Data: map[string]string{ | ||
"values": getValuesProxyDisabled(), | ||
}, | ||
}, | ||
result: false, | ||
}, | ||
{ | ||
name: "case 2", | ||
configMap: corev1.ConfigMap{ | ||
Data: map[string]string{ | ||
"values": getValuesProxyNotDefined(), | ||
}, | ||
}, | ||
result: false, | ||
}, | ||
{ | ||
name: "case 3", | ||
configMap: corev1.ConfigMap{ | ||
Data: map[string]string{ | ||
"values": getValuesProxyEmpty(), | ||
}, | ||
}, | ||
result: false, | ||
}, | ||
{ | ||
name: "case 4", | ||
configMap: corev1.ConfigMap{ | ||
Data: map[string]string{ | ||
"values": getValuesEmptyString(), | ||
}, | ||
}, | ||
result: false, | ||
}, | ||
{ | ||
name: "case 5", | ||
configMap: corev1.ConfigMap{ | ||
Data: map[string]string{ | ||
"values": getValuesRandomContent(), | ||
}, | ||
}, | ||
result: false, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "case 6", | ||
configMap: corev1.ConfigMap{ | ||
Data: map[string]string{ | ||
"wrongKey": getValuesProxyEnabled(), | ||
}, | ||
}, | ||
result: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
result, err := getProxyEnabledValueFromConfigMap(tc.configMap) | ||
if err != nil && !tc.expectError { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if err == nil && tc.expectError { | ||
t.Fatalf("expected error, got nil") | ||
} | ||
if result != tc.result { | ||
t.Fatalf("result == %#v, want %#v", result, tc.result) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func getValuesProxyEnabled() string { | ||
return `| | ||
global: | ||
release: | ||
version: 1.2.3 | ||
podSecurityStandards: | ||
enforced: true | ||
connectivity: | ||
baseDomain: test.example.io | ||
proxy: | ||
enabled: true | ||
httpProxy: http://proxy.example.io:3128 | ||
httpsProxy: http://proxy.example.io:3128 | ||
noProxy: localhost,example.io | ||
availabilityZoneUsageLimit: 3 | ||
providerSpecific: | ||
region: far-away-4` | ||
} | ||
|
||
func getValuesProxyDisabled() string { | ||
return `| | ||
global: | ||
release: | ||
version: 1.2.3 | ||
podSecurityStandards: | ||
enforced: true | ||
connectivity: | ||
baseDomain: test.example.io | ||
proxy: | ||
enabled: false | ||
availabilityZoneUsageLimit: 3 | ||
providerSpecific: | ||
region: far-away-4` | ||
} | ||
|
||
func getValuesProxyNotDefined() string { | ||
return `| | ||
global: | ||
release: | ||
version: 1.2.3 | ||
podSecurityStandards: | ||
enforced: true | ||
connectivity: | ||
baseDomain: test.example.io | ||
availabilityZoneUsageLimit: 3 | ||
providerSpecific: | ||
region: far-away-4` | ||
} | ||
|
||
func getValuesProxyEmpty() string { | ||
return `| | ||
global: | ||
release: | ||
version: 1.2.3 | ||
podSecurityStandards: | ||
enforced: true | ||
connectivity: | ||
baseDomain: test.example.io | ||
proxy: | ||
enabled: | ||
availabilityZoneUsageLimit: 3 | ||
providerSpecific: | ||
region: far-away-4` | ||
} | ||
|
||
func getValuesEmptyString() string { | ||
return `|` | ||
} | ||
|
||
func getValuesRandomContent() string { | ||
return `abcd | ||
efgh | ||
ijkl` | ||
} |