-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
92 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
hostNetwork: true | ||
containers: | ||
- image: kube-proxy:{{.KUBERNETES_VERSION}}-calico-hostprocess |
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,55 @@ | ||
package templates | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"os" | ||
|
||
"text/template" | ||
) | ||
|
||
type KubeProxyTmpl struct { | ||
KUBERNETES_VERSION string | ||
} | ||
|
||
type ConfigMapTmpl struct { | ||
KUBERNETES_SERVICE_HOST string | ||
KUBERNETES_SERVICE_PORT string | ||
} | ||
|
||
type SpecData struct { | ||
Spec struct { | ||
StrictAffinity bool `json:"strictAffinity,omitempty"` | ||
} `json:"spec,omitempty"` | ||
} | ||
|
||
// ChangeTemplate overwrite the pre-defined text template based in the input struct | ||
func ChangeTemplate[T KubeProxyTmpl | ConfigMapTmpl](mapping string, tmplStruct T) (string, error) { | ||
var result bytes.Buffer | ||
// Parse template and apply changes from the struct | ||
tmpl := template.Must(template.New("render").Parse(mapping)) | ||
if err := tmpl.Execute(&result, tmplStruct); err != nil { | ||
return "", err | ||
} | ||
// Returns the resulted rendering. | ||
return result.String(), nil | ||
} | ||
|
||
// OpenYAMLFile renders the YAML file and returns its content | ||
func OpenYAMLFile(filename string) (content []byte, err error) { | ||
var fd *os.File | ||
if fd, err = os.Open(filename); err != nil { | ||
return | ||
} | ||
content, err = io.ReadAll(fd) | ||
return | ||
} | ||
|
||
// GetSpecAffinity render affinity struct for Calico patch | ||
func GetSpecAffinity() (content []byte) { | ||
var data = SpecData{} | ||
data.Spec.StrictAffinity = true | ||
content, _ = json.Marshal(data) | ||
return | ||
} |
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,26 @@ | ||
package templates | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
var filepath = "./testdata/kube-proxy.yml" | ||
|
||
func TestOpenYAML(t *testing.T) { | ||
content, err := OpenYAMLFile(filepath) | ||
assert.Nil(t, err) | ||
assert.Greater(t, len(content), 0) | ||
assert.Contains(t, string(content), "{{.KUBERNETES_VERSION}}") | ||
} | ||
|
||
func TestRenderTemplate(t *testing.T) { | ||
content, err := OpenYAMLFile(filepath) | ||
assert.Nil(t, err) | ||
|
||
var version string = "v1.19.0" | ||
kpTmpl := KubeProxyTmpl{KUBERNETES_VERSION: version} | ||
output, err := ChangeTemplate(string(content), kpTmpl) | ||
assert.Nil(t, err) | ||
assert.Contains(t, output, version) | ||
} |
This file was deleted.
Oops, something went wrong.
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,8 @@ | ||
kind: ConfigMap | ||
apiVersion: v1 | ||
metadata: | ||
name: kubernetes-services-endpoint | ||
namespace: tigera-operator | ||
data: | ||
KUBERNETES_SERVICE_HOST: {{ .KUBERNETES_SERVICE_HOST }} | ||
KUBERNETES_SERVICE_PORT: {{ .KUBERNETES_SERVICE_PORT }} |
This file was deleted.
Oops, something went wrong.