Skip to content

Commit 0427d17

Browse files
authored
Merge pull request #130 from cmcga1125/envopts
Adding an environment vairiable to change default hostname only ingress abilities.
2 parents 83f2d2e + 65788d4 commit 0427d17

File tree

5 files changed

+79
-5
lines changed

5 files changed

+79
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ _See [helm upgrade](https://helm.sh/docs/helm/helm_upgrade/) for command documen
230230

231231
### Configurations
232232

233-
There are other variables that can be set to a different value. For list of all the modifiable variables/values, take a look at './deploy/chart/values.yaml'.
233+
There are other variables that can be set to a different value. For list of all the modifiable variables/values, take a look at './deploy/chart/values.yaml'.
234234

235235
Values can be set/overrided by using the '--set var=value,...' flag or by passing in a custom-values.yaml using '-f custom-values.yaml'.
236236

cloud/linode/loadbalancers.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"net/http"
8+
"os"
89
"strconv"
910
"strings"
1011
"time"
@@ -776,14 +777,26 @@ func makeLoadBalancerStatus(service *v1.Service, nb *linodego.NodeBalancer) *v1.
776777
Hostname: *nb.Hostname,
777778
}
778779
if !getServiceBoolAnnotation(service, annLinodeHostnameOnlyIngress) {
779-
ingress.IP = *nb.IPv4
780+
if val := envBoolOptions("LINODE_HOSTNAME_ONLY_INGRESS"); val {
781+
klog.Infof("LINODE_HOSTNAME_ONLY_INGRESS: (%v)", val)
782+
} else {
783+
ingress.IP = *nb.IPv4
784+
}
780785
}
781-
782786
return &v1.LoadBalancerStatus{
783787
Ingress: []v1.LoadBalancerIngress{ingress},
784788
}
785789
}
786790

791+
// Checks for a truth value in an environment variable
792+
func envBoolOptions(o string) bool {
793+
boolValue, err := strconv.ParseBool(os.Getenv(o))
794+
if err != nil {
795+
return false
796+
}
797+
return boolValue
798+
}
799+
787800
// getServiceNn returns the services namespaced name.
788801
func getServiceNn(service *v1.Service) string {
789802
return fmt.Sprintf("%s/%s", service.Namespace, service.Name)

cloud/linode/loadbalancers_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"net/http/httptest"
8+
"os"
89
"reflect"
910
"strconv"
1011
"testing"
@@ -166,6 +167,10 @@ func TestCCMLoadBalancers(t *testing.T) {
166167
name: "makeLoadBalancerStatus",
167168
f: testMakeLoadBalancerStatus,
168169
},
170+
{
171+
name: "makeLoadBalancerStatusEnvVar",
172+
f: testMakeLoadBalancerStatusEnvVar,
173+
},
169174
{
170175
name: "Cleanup does not call the API unless Service annotated",
171176
f: testCleanupDoesntCall,
@@ -1448,6 +1453,55 @@ func testMakeLoadBalancerStatus(t *testing.T, client *linodego.Client, _ *fakeAP
14481453
}
14491454
}
14501455

1456+
func testMakeLoadBalancerStatusEnvVar(t *testing.T, client *linodego.Client, _ *fakeAPI) {
1457+
ipv4 := "192.168.0.1"
1458+
hostname := "nb-192-168-0-1.newark.nodebalancer.linode.com"
1459+
nb := &linodego.NodeBalancer{
1460+
IPv4: &ipv4,
1461+
Hostname: &hostname,
1462+
}
1463+
1464+
svc := &v1.Service{
1465+
ObjectMeta: metav1.ObjectMeta{
1466+
Name: "test",
1467+
Annotations: make(map[string]string, 1),
1468+
},
1469+
}
1470+
1471+
expectedStatus := &v1.LoadBalancerStatus{
1472+
Ingress: []v1.LoadBalancerIngress{{
1473+
Hostname: hostname,
1474+
IP: ipv4,
1475+
}},
1476+
}
1477+
status := makeLoadBalancerStatus(svc, nb)
1478+
if !reflect.DeepEqual(status, expectedStatus) {
1479+
t.Errorf("expected status for basic service to be %#v; got %#v", expectedStatus, status)
1480+
}
1481+
1482+
os.Setenv("LINODE_HOSTNAME_ONLY_INGRESS", "true")
1483+
expectedStatus.Ingress[0] = v1.LoadBalancerIngress{Hostname: hostname}
1484+
status = makeLoadBalancerStatus(svc, nb)
1485+
if !reflect.DeepEqual(status, expectedStatus) {
1486+
t.Errorf("expected status for %q annotated service to be %#v; got %#v", annLinodeHostnameOnlyIngress, expectedStatus, status)
1487+
}
1488+
1489+
os.Setenv("LINODE_HOSTNAME_ONLY_INGRESS", "false")
1490+
expectedStatus.Ingress[0] = v1.LoadBalancerIngress{Hostname: hostname}
1491+
status = makeLoadBalancerStatus(svc, nb)
1492+
if reflect.DeepEqual(status, expectedStatus) {
1493+
t.Errorf("expected status for %q annotated service to be %#v; got %#v", annLinodeHostnameOnlyIngress, expectedStatus, status)
1494+
}
1495+
1496+
os.Setenv("LINODE_HOSTNAME_ONLY_INGRESS", "banana")
1497+
expectedStatus.Ingress[0] = v1.LoadBalancerIngress{Hostname: hostname}
1498+
status = makeLoadBalancerStatus(svc, nb)
1499+
if reflect.DeepEqual(status, expectedStatus) {
1500+
t.Errorf("expected status for %q annotated service to be %#v; got %#v", annLinodeHostnameOnlyIngress, expectedStatus, status)
1501+
}
1502+
os.Unsetenv("LINODE_HOSTNAME_ONLY_INGRESS")
1503+
}
1504+
14511505
func testCleanupDoesntCall(t *testing.T, client *linodego.Client, fakeAPI *fakeAPI) {
14521506
region := "us-west"
14531507
nb1, err := client.CreateNodeBalancer(context.TODO(), linodego.NodeBalancerCreateOptions{Region: region})

deploy/chart/templates/daemonset.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ spec:
4747
secretKeyRef:
4848
name: ccm-linode
4949
key: region
50+
{{- toYaml .Values.env | nindent 12 }}
5051
volumes:
5152
- name: k8s
5253
hostPath:

deploy/chart/values.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# apiToken [Required] - Must be a Linode APIv4 Personal Access Token with all permissions. (https://cloud.linode.com/profile/tokens)
1+
# apiToken [Required] - Must be a Linode APIv4 Personal Access Token with all permissions. (https://cloud.linode.com/profile/tokens)
22
apiToken: ""
33

44
# region [Required] - Must be a Linode region. (https://api.linode.com/v4/regions)
@@ -20,7 +20,7 @@ image:
2020
namespace: "kube-system"
2121

2222
# Set of default tolerations
23-
tolerations:
23+
tolerations:
2424
# The CCM can run on Nodes tainted as masters
2525
- key: "node-role.kubernetes.io/master"
2626
effect: "NoSchedule"
@@ -37,3 +37,9 @@ tolerations:
3737
- key: node.kubernetes.io/unreachable
3838
operator: Exists
3939
effect: NoSchedule
40+
# This section adds the ability to pass environment variables to adjust CCM defaults
41+
# https://github.com/linode/linode-cloud-controller-manager/blob/master/cloud/linode/loadbalancers.go
42+
# LINODE_HOSTNAME_ONLY_INGRESS type bool is supported
43+
# env:
44+
# - name: EXAMPLE_ENV_VAR
45+
# value: "true"

0 commit comments

Comments
 (0)