-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The CloudInit controller will reconcile CloudInit resources (introduced with previous patches to add a webhook for the resource.) It also places an inotify watch on `/oem` so that any local modifications are also subject to reconciliation. Signed-off-by: Connor Kuehl <[email protected]>
- Loading branch information
Showing
431 changed files
with
4,678 additions
and
3,498 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package cloudinit | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
|
||
cloudinitv1 "github.com/harvester/node-manager/pkg/apis/node.harvesterhci.io/v1beta1" | ||
) | ||
|
||
const AnnotationHash = "node.harvesterhci.io/cloudinit-hash" | ||
|
||
var Directory = "/host/oem" | ||
|
||
// RequireLocal ensures that the Elemental cloud-init file described by | ||
// the given `cloudinit` object is an exact copy of the `cloudinit` object's | ||
// contents. | ||
func RequireLocal(cloudinit *cloudinitv1.CloudInit) (bool, error) { | ||
absPath := filepath.Join(Directory, cloudinit.Spec.Filename) | ||
|
||
f, err := os.Open(absPath) | ||
var r io.Reader = f | ||
if err != nil { | ||
r = strings.NewReader("") | ||
} else { | ||
defer f.Close() | ||
} | ||
|
||
diskChecksum, err := Measure(r) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if fmt.Sprintf("%x", diskChecksum) == cloudinit.Annotations[AnnotationHash] { | ||
return false, nil | ||
} | ||
|
||
tempFile, err := os.CreateTemp(Directory, "node-manager") | ||
if err != nil { | ||
return false, err | ||
} | ||
defer os.RemoveAll(tempFile.Name()) | ||
defer tempFile.Close() | ||
|
||
_, err = io.Copy(tempFile, strings.NewReader(cloudinit.Spec.Contents)) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
err = os.Rename(tempFile.Name(), absPath) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func Measure(r io.Reader) ([]byte, error) { | ||
h := sha256.New() | ||
_, err := io.Copy(h, r) | ||
return h.Sum(nil), err | ||
} | ||
|
||
func MatchesNode(node *corev1.Node, cloudinit *cloudinitv1.CloudInit) bool { | ||
selector := labels.SelectorFromSet(labels.Set(cloudinit.Spec.MatchSelector)) | ||
return selector.Matches(labels.Set(node.GetLabels())) | ||
} |
Oops, something went wrong.