Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature Escape-Html on template #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions configmaps/vault-template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ metadata:
konfd.io/kind: configmap
konfd.io/name: vault
konfd.io/key: server.hcl
konfd.io/escape-html: "true"
labels:
konfd.io/template: "true"
data:
Expand Down
43 changes: 38 additions & 5 deletions kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ type Metadata struct {
Annotations map[string]string `json:"annotations,omitempty"`
}

func (c * ConfigMap) jsonMarshallIndentHtmlEscape() ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(c)
var buf bytes.Buffer
err = json.Indent(&buf, buffer.Bytes(), "", "")
if err != nil {
return nil, err
}
return buf.Bytes(), err
}


func getConfigMap(namespace, name string) (*ConfigMap, error) {
u := fmt.Sprintf("http://127.0.0.1:8001/api/v1/namespaces/%s/configmaps/%s", namespace, name)
resp, err := http.Get(u)
Expand Down Expand Up @@ -178,8 +192,17 @@ func newSecret(namespace, name, key, value string) *Secret {
return s
}

func createConfigMap(c *ConfigMap) error {
body, err := json.MarshalIndent(&c, "", " ")
func createConfigMap(c *ConfigMap,escapeHtml bool) error {
if (escapeHtml == true){
body, err := json.MarshalIndent(&c, "", " ")
return handleCreateConfigMap(c,body,err)
}else{
body, err := c.jsonMarshallIndentHtmlEscape()
return handleCreateConfigMap(c,body,err)
}
}

func handleCreateConfigMap(c *ConfigMap, body []byte, err error) error {
if err != nil {
return fmt.Errorf("error encoding configmap %s: %v", c.Metadata.Name, err)
}
Expand All @@ -194,12 +217,12 @@ func createConfigMap(c *ConfigMap) error {
if resp.StatusCode != 201 {
return fmt.Errorf("error creating configmap %s; got HTTP %v status code", c.Metadata.Name, resp.StatusCode)
}

return nil
}

func createSecret(s *Secret) error {
body, err := json.MarshalIndent(&s, "", " ")

if err != nil {
return fmt.Errorf("error encoding secret %s: %v", s.Metadata.Name, err)
}
Expand All @@ -218,8 +241,18 @@ func createSecret(s *Secret) error {
return nil
}

func updateConfigMap(c *ConfigMap) error {
body, err := json.MarshalIndent(&c, "", " ")
func updateConfigMap(c *ConfigMap,escapeHtml bool) error {

if (escapeHtml == true){
body, err := json.MarshalIndent(&c, "", " ")
return handleUpdateConfigMap(c,body,err)
}else{
body, err := c.jsonMarshallIndentHtmlEscape()
return handleUpdateConfigMap(c,body,err)
}
}

func handleUpdateConfigMap(c *ConfigMap, body []byte, err error) error{
if err != nil {
return fmt.Errorf("error encoding configmap %s: %v", c.Metadata.Name, err)
}
Expand Down
13 changes: 9 additions & 4 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,25 +167,30 @@ func (tp *TemplateProcessor) processConfigMapTemplate(configmap *ConfigMap) erro
name := annotations["konfd.io/name"]
key := annotations["konfd.io/key"]
kind := annotations["konfd.io/kind"]
escapeHtmlStr := annotations["konfd.io/escape-html"]
escapeHtml :=true
if escapeHtmlStr == "false" {
escapeHtml=false
}

switch kind {
case "configmap":
return tp.processConfigMap(tp.namespace, name, key, value.String())
return tp.processConfigMap(tp.namespace, name, key, value.String(),escapeHtml)
case "secret":
return tp.processSecret(tp.namespace, name, key, value.String())
}
return nil
}

func (tp *TemplateProcessor) processConfigMap(namespace, name, key, value string) error {
func (tp *TemplateProcessor) processConfigMap(namespace, name, key, value string, escapeHtml bool) error {
cm := newConfigMap(namespace, name, key, value)

ccm, err := getConfigMap(namespace, name)
if err == ErrNotExist {
if tp.noop {
return printObject(cm)
}
return createConfigMap(cm)
return createConfigMap(cm,escapeHtml)
}

if err != nil {
Expand All @@ -200,7 +205,7 @@ func (tp *TemplateProcessor) processConfigMap(namespace, name, key, value string
if tp.noop {
return printObject(ccm)
}
return updateConfigMap(ccm)
return updateConfigMap(ccm,escapeHtml)
}

func (tp *TemplateProcessor) processSecret(namespace, name, key, value string) error {
Expand Down