Skip to content

Commit

Permalink
[WIP] Static IP addresses support were added for onpremises Cassandra
Browse files Browse the repository at this point in the history
  • Loading branch information
testisnullus committed Nov 9, 2023
1 parent 9302179 commit 4ffb2cd
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 2 deletions.
7 changes: 7 additions & 0 deletions apis/clusters/v1beta1/cassandra_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ type CassandraOnPremisesSpec struct {
NodeMemory string `json:"nodeMemory"`
OSImageURL string `json:"osImageURL"`
CloudInitScriptNamespacedName *NamespacedName `json:"cloudInitScriptNamespacedName"`
StaticIPs *StaticIPs `json:"staticIPs,omitempty"`
CNIType string `json:"cniType,omitempty"`
}

type StaticIPs struct {
SSHGateway string `json:"sshGateway"`
Nodes []string `json:"nodes"`
}

// CassandraStatus defines the observed state of Cassandra
Expand Down
22 changes: 22 additions & 0 deletions apis/clusters/v1beta1/cassandra_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1beta1
import (
"context"
"fmt"
"net"
"regexp"

"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -89,6 +90,17 @@ func (cv *cassandraValidator) ValidateCreate(ctx context.Context, obj runtime.Ob
}

if c.Spec.OnPremisesSpec != nil {
if len(c.Spec.OnPremisesSpec.StaticIPs.Nodes) != c.Spec.DataCentres[0].NodesNumber {
return fmt.Errorf("mismatch between the number of nodes and the number of IP addresses")
}

for _, ip := range c.Spec.OnPremisesSpec.StaticIPs.Nodes {
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
return fmt.Errorf("%s is not a valid IP address. Please provide the IP without a network mask", parsedIP)
}
}

osDiskSizeMatched, err := regexp.Match(models.StorageRegExp, []byte(c.Spec.OnPremisesSpec.OSDiskSize))
if !osDiskSizeMatched || err != nil {
return fmt.Errorf("disk size field for node OS must fit pattern: %s",
Expand Down Expand Up @@ -116,6 +128,16 @@ func (cv *cassandraValidator) ValidateCreate(ctx context.Context, obj runtime.Ob
return fmt.Errorf("ssh gateway memory field must fit pattern: %s",
models.MemoryRegExp)
}

if c.Spec.OnPremisesSpec.StaticIPs.SSHGateway == "" {
return fmt.Errorf("ssh gateway private IP is not specified")
}

parsedIP := net.ParseIP(c.Spec.OnPremisesSpec.StaticIPs.SSHGateway)
if parsedIP == nil {
return fmt.Errorf("%s is not a valid IP address. Please provide the IP without a network mask", parsedIP)
}

}
}

Expand Down
25 changes: 25 additions & 0 deletions apis/clusters/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions config/crd/bases/clusters.instaclustr.com_cassandras.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ spec:
type: integer
sshGatewayMemory:
type: string
staticIPs:
properties:
nodes:
items:
type: string
type: array
sshGateway:
type: string
required:
- nodes
- sshGateway
type: object
storageClassName:
type: string
required:
Expand All @@ -146,6 +158,7 @@ spec:
- nodeMemory
- osDiskSize
- osImageURL
- staticIPs
- storageClassName
type: object
passwordAndUserAuth:
Expand Down
7 changes: 7 additions & 0 deletions config/samples/clusters_v1beta1_cassandra.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ spec:
cloudInitScriptNamespacedName:
namespace: default
name: instaclustr-cloud-init-secret
cniType: "Calico"
staticIPs:
sshGateway: "192.168.150.1"
nodes:
- "192.168.150.2"
- "192.168.150.3"
- "192.168.150.4"
dataCentres:
- name: "onPremCassandra"
region: "CLIENT_DC"
Expand Down
9 changes: 7 additions & 2 deletions controllers/clusters/cassandra_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ type CassandraReconciler struct {
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=cassandras/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=clusters.instaclustr.com,resources=cassandras/finalizers,verbs=update
//+kubebuilder:rbac:groups="",resources=events,verbs=create;patch
//+virtualmachineinstance.kubevirt.io/node-vm-2-cassandra-cluster:rbac:groups="",resources=endpoints,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=cdi.kubevirt.io,resources=datavolumes,verbs=get;list;watch;create;update;patch;delete;deletecollection
//+kubebuilder:rbac:groups=kubevirt.io,resources=virtualmachines,verbs=get;list;watch;create;update;patch;delete;deletecollection
//+kubebuilder:rbac:groups=kubevirt.io,resources=virtualmachineinstances,verbs=get;list;watch;create;update;patch;delete;deletecollection
Expand Down Expand Up @@ -1857,6 +1856,7 @@ func (r *CassandraReconciler) reconcileSSHGatewayResources(
gateway.Rack,
gatewayDV.Name,
secretName,
c.Spec.OnPremisesSpec.StaticIPs.SSHGateway,
gatewayCPU,
gatewayMemory)
if err != nil {
Expand Down Expand Up @@ -1954,6 +1954,7 @@ func (r *CassandraReconciler) reconcileNodesResources(
node.Rack,
nodeOSDV.Name,
secretName,
c.Spec.OnPremisesSpec.StaticIPs.Nodes[i],
nodeCPU,
nodeMemory,
nodeDataDV.Name)
Expand Down Expand Up @@ -2255,7 +2256,8 @@ func (r *CassandraReconciler) newVM(
nodeID,
nodeRack,
OSDiskDVName,
ignitionSecretName string,
ignitionSecretName,
staticIP string,
cpu,
memory resource.Quantity,
storageDVNames ...string,
Expand Down Expand Up @@ -2298,6 +2300,9 @@ func (r *CassandraReconciler) newVM(
models.NodeRackLabel: nodeRack,
models.KubevirtDomainLabel: vmName,
},
Annotations: map[string]string{
"cni.projectcalico.org/ipAddrs": fmt.Sprintf("[\"%s\"]", staticIP),
},
},
Spec: virtcorev1.VirtualMachineInstanceSpec{
Hostname: vmName,
Expand Down

0 comments on commit 4ffb2cd

Please sign in to comment.