Skip to content

Commit

Permalink
cmd/deploy: Support ingress port flag (#382)
Browse files Browse the repository at this point in the history
Support ingress port flag for the deploy peer command.

Signed-off-by: Kfir Toledo <[email protected]>
  • Loading branch information
kfirtoledo authored Mar 11, 2024
1 parent accde7f commit 6b9fe17
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
40 changes: 30 additions & 10 deletions cmd/cl-adm/cmd/deploy/deploy_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type PeerOptions struct {
StartInstance bool
// Ingress, represents the type of service used to expose the ClusterLink deployment.
Ingress string
// IngressPort, represents the port number of the service used to expose the ClusterLink deployment.
IngressPort uint16
// ContainerRegistry is the container registry to pull the project images.
ContainerRegistry string
}
Expand Down Expand Up @@ -80,13 +82,18 @@ func NewCmdDeployPeer() *cobra.Command {
func (o *PeerOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.Name, "name", "", "Peer name.")
fs.StringVar(&o.CertDir, "cert-dir", ".", "The directory where the certificates for the fabric and peer are located.")
fs.BoolVar(&o.StartInstance, "start", false, "If true, deploy a ClusterLink instance that will create ClusterLink components")
fs.StringVar(&o.Namespace, "start-namespace", app.SystemNamespace,
"Namespace where the ClusterLink components are deployed if --start is set.")
fs.StringVar(&o.Ingress, "start-ingress", string(apis.IngressTypeLoadBalancer),
" By default, it is set to 443 for LoadBalancer and a random port (3000 to 32767) for NodePort, if --start is set.")
fs.StringVar(&o.Namespace, "namespace", app.SystemNamespace,
"Namespace where the ClusterLink components are deployed.")
fs.StringVar(&o.ContainerRegistry, "container-registry", config.DefaultRegistry,
"The container registry to pull the project images.")
fs.BoolVar(&o.StartInstance, "autostart", false,
"If false, it will deploy only the ClusteLink operator and ClusterLink K8s secrets."+
"If true, it will also deploy the ClusterLink instance CRD, which will create the ClusterLink components.")
fs.StringVar(&o.Ingress, "ingress", string(apis.IngressTypeLoadBalancer), "Represents the type of service used"+
"to expose the ClusterLink deployment (LoadBalancer/NodePort/none). This option is only valid if --autostart is set.")
fs.Uint16Var(&o.IngressPort, "ingress-port", apis.DefaultExternalPort,
"Represents the ingress port. By default it is set to 443 for LoadBalancer"+
" and a random port in range (30000 to 32767) for NodePort. This option is only valid if --autostart is set.")
}

// RequiredFlags are the names of flags that must be explicitly specified.
Expand Down Expand Up @@ -121,7 +128,7 @@ func (o *PeerOptions) Run() error {
}

managerModified := strings.ReplaceAll(string(managerFile), ghImage, newImage)
err = decoder.DecodeEach(context.Background(), strings.NewReader(managerModified), decoder.CreateHandler(resource))
err = decoder.DecodeEach(context.Background(), strings.NewReader(managerModified), decoder.CreateIgnoreAlreadyExists(resource))
if err != nil {
return err
}
Expand All @@ -143,7 +150,7 @@ func (o *PeerOptions) Run() error {
err = decoder.DecodeEach(
context.Background(),
strings.NewReader(string(secretFile)),
decoder.CreateHandler(resource),
decoder.CreateIgnoreAlreadyExists(resource),
decoder.MutateNamespace(o.Namespace),
)
if err != nil {
Expand All @@ -152,23 +159,36 @@ func (o *PeerOptions) Run() error {

// Create ClusterLink instance
if o.StartInstance {
instance, err := platform.K8SClusterLinkInstanceConfig(&platform.Config{
cfg := &platform.Config{
Peer: o.Name,
Dataplanes: 1,
DataplaneType: platform.DataplaneTypeEnvoy,
LogLevel: "info",
ContainerRegistry: o.ContainerRegistry,
Namespace: o.Namespace,
IngressType: o.Ingress,
}, "cl-instance")
}
if o.IngressPort != apis.DefaultExternalPort {
cfg.IngressPort = o.IngressPort
}
instance, err := platform.K8SClusterLinkInstanceConfig(cfg, "cl-instance")
if err != nil {
return err
}

err = decoder.DecodeEach(context.Background(), strings.NewReader(string(instance)), decoder.CreateHandler(resource))
if err != nil {
if errors.IsAlreadyExists(err) {
fmt.Println("CRD instance for ClusterLink (\"cl-instance\") was already exist.")
} else if err != nil {
return err
}
} else {
if o.Ingress != string(apis.IngressTypeLoadBalancer) {
fmt.Println("flag --autostart is not set, ignoring --ingres flag")
}
if o.IngressPort != apis.DefaultExternalPort {
fmt.Println("flag --autostart is not set, ignoring --ingres-port flag")
}
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/clusterlink.net/v1alpha1/instance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ const (
)

const (
// ExternalPort represents the default value for the external ingress service of the LoadBalancer type.
ExternalPort = 443
// DefaultExternalPort represents the default value for the external ingress service.
DefaultExternalPort = 443
)

// ComponentStatus defines the status of component in ClusterLink.
Expand Down
2 changes: 2 additions & 0 deletions pkg/bootstrap/platform/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type Config struct {
ContainerRegistry string
// IngressType is the type of ingress to create.
IngressType string
// IngressPort is the ingress port number to create.
IngressPort uint16
// CRDMode indicates a CRD-based controlplane.
CRDMode bool
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/bootstrap/platform/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

cpapp "github.com/clusterlink-net/clusterlink/cmd/cl-controlplane/app"
dpapp "github.com/clusterlink-net/clusterlink/cmd/cl-dataplane/app"
apis "github.com/clusterlink-net/clusterlink/pkg/apis/clusterlink.net/v1alpha1"
cpapi "github.com/clusterlink-net/clusterlink/pkg/controlplane/api"
dpapi "github.com/clusterlink-net/clusterlink/pkg/dataplane/api"
)
Expand Down Expand Up @@ -316,6 +317,9 @@ spec:
replicas: {{.dataplanes}}
ingress:
type: {{.ingressType}}
{{- if .ingressPort }}
port: {{.ingressPort }}
{{ end }}
logLevel: {{.logLevel}}
containerRegistry: {{.containerRegistry}}
namespace: {{.namespace}}
Expand Down Expand Up @@ -413,6 +417,12 @@ func K8SClusterLinkInstanceConfig(config *Config, name string) ([]byte, error) {
"ingressType": config.IngressType,
}

if config.IngressPort != 0 {
if config.IngressType == string(apis.IngressTypeNodePort) && (config.IngressPort < 30000) || (config.IngressPort > 32767) {
return nil, fmt.Errorf("nodeport number %v is not in the valid range (30000:32767)", config.IngressPort)
}
args["ingressPort"] = config.IngressPort
}
var clConfig bytes.Buffer
t := template.Must(template.New("").Parse(ClusterLinkInstanceTemplate))
if err := t.Execute(&clConfig, args); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/controller/instance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func (r *InstanceReconciler) createExternalService(ctx context.Context, instance
},
Ports: []corev1.ServicePort{
{
Port: clusterlink.ExternalPort,
Port: clusterlink.DefaultExternalPort,
TargetPort: intstr.FromInt(dpapi.ListenPort),
Name: "https",
},
Expand Down

0 comments on commit 6b9fe17

Please sign in to comment.