Skip to content

Commit

Permalink
Fix nfs mount opts
Browse files Browse the repository at this point in the history
  • Loading branch information
QcFe authored and kingmakerbot committed May 16, 2024
1 parent d6bc8fb commit 5f8d88b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
3 changes: 3 additions & 0 deletions operators/pkg/forge/cloudinit-startup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
mkdir -p "$NFSPATH"
chown 1000:1000 "$NFSPATH"
13 changes: 12 additions & 1 deletion operators/pkg/forge/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package forge

import (
"bytes"
_ "embed"
"fmt"

"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -49,6 +51,15 @@ type interf struct {
DHCP4 bool `yaml:"dhcp4"`
}

//go:embed cloudinit-startup.sh
var scriptdata []byte

// CloudInitUserScriptData configures and forges the cloud-init startup script.
func CloudInitUserScriptData() ([]byte, error) {
userScriptData := bytes.ReplaceAll(scriptdata, []byte("$NFSPATH"), []byte(MyDriveVolumeMountPath))
return userScriptData, nil
}

// CloudInitUserData forges the yaml manifest representing the cloud-init userdata configuration.
func CloudInitUserData(nfsServerName, nfsPath string, publicKeys []string) ([]byte, error) {
config := userdata{
Expand All @@ -73,7 +84,7 @@ func CloudInitUserData(nfsServerName, nfsPath string, publicKeys []string) ([]by
fmt.Sprintf("%s:%s", nfsServerName, nfsPath),
MyDriveVolumeMountPath,
"nfs",
"rw,tcp,hard,intr,rsize=8192,wsize=8192,timeo=14",
"rw,tcp,hard,intr,rsize=8192,wsize=8192,timeo=14,_netdev,user",
"0",
"0",
}}
Expand Down
20 changes: 18 additions & 2 deletions operators/pkg/forge/cloudinit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/netgroup-polito/CrownLabs/operators/pkg/forge"
)

var _ = Describe("CloudInit userdata generation", func() {
var _ = Describe("CloudInit files generation", func() {
Context("The CloudInitUserData function", func() {
const (
serviceName = "rook-ceph-nfs-my-nfs-a.rook-ceph.svc.cluster.local"
Expand All @@ -48,7 +48,7 @@ mounts:
- - rook-ceph-nfs-my-nfs-a.rook-ceph.svc.cluster.local:/path
- /media/mydrive
- nfs
- rw,tcp,hard,intr,rsize=8192,wsize=8192,timeo=14
- rw,tcp,hard,intr,rsize=8192,wsize=8192,timeo=14,_netdev,user
- "0"
- "0"
ssh_authorized_keys:
Expand All @@ -74,4 +74,20 @@ ssh_authorized_keys:
It("Should succeed", func() { Expect(err).ToNot(HaveOccurred()) })
It("Should match the expected output", func() { Expect(output).To(WithTransform(Transformer, Equal(Transformer([]byte(expected))))) })
})

Context("The CloudInitUserScriptData function", func() {
const expected = `#!/bin/bash
mkdir -p "/media/mydrive"
chown 1000:1000 "/media/mydrive"
`

var (
scriptdata []byte
err error
)
JustBeforeEach(func() { scriptdata, err = forge.CloudInitUserScriptData() })

It("Should succeed", func() { Expect(err).ToNot(HaveOccurred()) })
It("Should match the expected output", func() { Expect(scriptdata).To(Equal([]byte(expected))) })
})
})
9 changes: 8 additions & 1 deletion operators/pkg/instctrl/cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,19 @@ func (r *InstanceReconciler) EnforceCloudInitSecret(ctx context.Context) error {
log.Error(err, "unable to marshal secret content")
return err
}

userScriptData, err := forge.CloudInitUserScriptData()
if err != nil {
log.Error(err, "unable to marshal secret content")
return err
}

// Enforce the cloud-init secret presence.
instance := clctx.InstanceFrom(ctx)
secret := corev1.Secret{ObjectMeta: forge.ObjectMeta(instance)}
res, err := ctrl.CreateOrUpdate(ctx, r.Client, &secret, func() error {
secret.SetLabels(forge.InstanceObjectLabels(secret.GetLabels(), instance))
secret.Data = map[string][]byte{UserDataKey: userdata}
secret.Data = map[string][]byte{UserDataKey: userdata, "x-shellscript": userScriptData}
secret.Type = corev1.SecretTypeOpaque
return ctrl.SetControllerReference(instance, &secret, r.Scheme)
})
Expand Down

0 comments on commit 5f8d88b

Please sign in to comment.