Skip to content

Commit

Permalink
management config xml to yaml (#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
omcal committed Jul 31, 2023
1 parent 51f0ba9 commit 3e9df26
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Test_clusterAddCommand(t *testing.T) {
},
},
},
want: "./bin/mc-conf.sh cluster add --lenient=true -H /data --client-config /config/dev.xml",
want: "./bin/mc-conf.sh cluster add --lenient=true -H /data --client-config /config/dev.yaml",
},
{
name: "Two Clusters Defined",
Expand All @@ -51,7 +51,7 @@ func Test_clusterAddCommand(t *testing.T) {
},
},
},
want: "./bin/mc-conf.sh cluster add --lenient=true -H /data --client-config /config/dev.xml && ./bin/mc-conf.sh cluster add --lenient=true -H /data --client-config /config/prod.xml",
want: "./bin/mc-conf.sh cluster add --lenient=true -H /data --client-config /config/dev.yaml && ./bin/mc-conf.sh cluster add --lenient=true -H /data --client-config /config/prod.yaml",
},
}
for _, tt := range tests {
Expand Down
96 changes: 38 additions & 58 deletions controllers/managementcenter/managementcenter_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/pem"
"encoding/xml"
"fmt"
"path"
"strings"
Expand All @@ -13,6 +12,7 @@ import (
"github.com/go-logr/logr"
routev1 "github.com/openshift/api/route/v1"
"github.com/pavlo-v-chernykh/keystore-go/v4"
"gopkg.in/yaml.v3"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -341,7 +341,7 @@ func (r *ManagementCenterReconciler) reconcileSecret(ctx context.Context, mc *ha
if err != nil {
return err
}
files[cluster.Name+".xml"] = clientConfig
files[cluster.Name+".yaml"] = clientConfig
}
secret.Data = files
return nil
Expand Down Expand Up @@ -504,7 +504,7 @@ func env(mc *hazelcastv1alpha1.ManagementCenter) []v1.EnvVar {
func clusterAddCommand(mc *hazelcastv1alpha1.ManagementCenter) string {
var commands []string
for _, cluster := range mc.Spec.HazelcastClusters {
commands = append(commands, fmt.Sprintf("./bin/mc-conf.sh cluster add --lenient=true -H /data --client-config %s", path.Join("/config", cluster.Name+".xml")))
commands = append(commands, fmt.Sprintf("./bin/mc-conf.sh cluster add --lenient=true -H /data --client-config %s", path.Join("/config", cluster.Name+".yaml")))
}
return strings.Join(commands, " && ")
}
Expand Down Expand Up @@ -587,25 +587,22 @@ func decodePEM(data []byte, typ string) ([]byte, error) {
}

func hazelcastClientConfig(ctx context.Context, c client.Client, config *hazelcastv1alpha1.HazelcastClusterConfig) ([]byte, error) {
clientConfig := HazelcastClient{
XMLNS: "http://www.hazelcast.com/schema/client-config",
XMLNSXSI: "http://www.w3.org/2001/XMLSchema-instance",
SchemaLocation: "http://www.hazelcast.com/schema/client-config http://www.hazelcast.com/schema/client-config/hazelcast-client-config-4.0.xsd",
ClusterName: config.Name,
clientConfig := HazelcastClientWrapper{HazelcastClient{
ClusterName: config.Name,
Network: Network{
ClusterMembers: []ClusterMembers{{
Address: config.Address,
}},
ClusterMembers: []string{
config.Address,
},
SSL: SSL{
Enabled: "false",
Enabled: false,
FactoryClassName: "com.hazelcast.nio.ssl.BasicSSLContextFactory",
},
},
}
}}

if config.TLS != nil && config.TLS.SecretName != "" {
clientConfig.Network.SSL = SSL{
Enabled: "true",
clientConfig.HazelcastClient.Network.SSL = SSL{
Enabled: true,
FactoryClassName: "com.hazelcast.nio.ssl.BasicSSLContextFactory",
Properties: NewSSLProperties(
path.Join("/config", config.Name+".jks"),
Expand All @@ -615,69 +612,52 @@ func hazelcastClientConfig(ctx context.Context, c client.Client, config *hazelca
}

var b bytes.Buffer
b.WriteString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
enc := xml.NewEncoder(&b)
enc.Indent("", " ")
enc := yaml.NewEncoder(&b)
if err := enc.Encode(clientConfig); err != nil {
return nil, err
}

if err := enc.Close(); err != nil {
return nil, err
}
return b.Bytes(), nil
}

type HazelcastClient struct {
XMLName xml.Name `xml:"hazelcast-client"`
XMLNS string `xml:"xmlns,attr"`
XMLNSXSI string `xml:"xmlns:xsi,attr"`
SchemaLocation string `xml:"xsi:schemaLocation,attr"`
ClusterName string `xml:"cluster-name"`
Network Network `xml:"network"`
type HazelcastClientWrapper struct {
HazelcastClient HazelcastClient `yaml:"hazelcast-client"`
}

type Network struct {
ClusterMembers []ClusterMembers `xml:"cluster-members"`
SSL SSL `xml:"ssl,omitempty"`
type HazelcastClient struct {
ClusterName string `yaml:"cluster-name"`
Network Network `yaml:"network"`
}

type ClusterMembers struct {
Address string `xml:"address"`
type Network struct {
ClusterMembers []string `yaml:"cluster-members,omitempty"`
SSL SSL `yaml:"ssl,omitempty,omitempty"`
}

type SSL struct {
Enabled string `xml:"enabled,attr"`
FactoryClassName string `xml:"factory-class-name"`
Properties Properties `xml:"properties"`
}

type Properties struct {
Properties []Property `xml:"property"`
Enabled bool `yaml:"enabled"`
FactoryClassName string `yaml:"factory-class-name"`
Properties map[string]string `yaml:"properties"`
}

type Property struct {
Text string `xml:",chardata"`
Name string `xml:"name,attr"`
}

func NewSSLProperties(path string, auth v1alpha1.MutualAuthentication) Properties {
func NewSSLProperties(path string, auth v1alpha1.MutualAuthentication) map[string]string {
const pass = "hazelcast"
switch auth {
case v1alpha1.MutualAuthenticationRequired:
return Properties{
Properties: []Property{
{Name: "protocol", Text: "TLSv1.2"},
{Name: "trustStore", Text: path},
{Name: "trustStorePassword", Text: pass},
{Name: "keyStore", Text: path},
{Name: "keyStorePassword", Text: pass},
},
return map[string]string{
"protocol": "TLSv1.2",
"keyStore": path,
"keyStorePassword": pass,
"trustStore": path,
"trustStorePassword": pass,
}
default:
return Properties{
Properties: []Property{
{Name: "protocol", Text: "TLSv1.2"},
{Name: "trustStore", Text: path},
{Name: "trustStorePassword", Text: pass},
},
return map[string]string{
"protocol": "TLSv1.2",
"trustStore": path,
"trustStorePassword": pass,
}
}
}
2 changes: 1 addition & 1 deletion test/integration/managementcenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ var _ = Describe("ManagementCenter CR", func() {
for _, env := range el {
if env.Name == "MC_INIT_CMD" {
for _, cl := range hzcl {
Expect(env.Value).To(ContainSubstring(fmt.Sprintf("--client-config /config/%s.xml", cl.Name)))
Expect(env.Value).To(ContainSubstring(fmt.Sprintf("--client-config /config/%s.yaml", cl.Name)))

}
}
Expand Down

0 comments on commit 3e9df26

Please sign in to comment.