Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove use of k8s configmap #2541

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 51 additions & 22 deletions src/pkg/cluster/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,24 @@ func (c *Cluster) StopInjectionMadness(ctx context.Context) error {
}

// Remove the configmaps
labelMatch := map[string]string{"zarf-injector": "payload"}
if err := c.DeleteConfigMapsByLabel(ctx, ZarfNamespaceName, labelMatch); err != nil {
selector, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
MatchLabels: map[string]string{
"zarf-injector": "payload",
},
})
if err != nil {
return err
}
listOpts := metav1.ListOptions{
LabelSelector: selector.String(),
}
err = c.Clientset.CoreV1().ConfigMaps(ZarfNamespaceName).DeleteCollection(ctx, metav1.DeleteOptions{}, listOpts)
if err != nil {
return err
}

// Remove the injector service
err := c.Clientset.CoreV1().Services(ZarfNamespaceName).Delete(ctx, "zarf-injector", metav1.DeleteOptions{})
err = c.Clientset.CoreV1().Services(ZarfNamespaceName).Delete(ctx, "zarf-injector", metav1.DeleteOptions{})
if err != nil {
return err
}
Expand Down Expand Up @@ -231,16 +242,26 @@ func (c *Cluster) createPayloadConfigMaps(ctx context.Context, seedImagesDir, ta
// Create a cat-friendly filename
fileName := fmt.Sprintf("zarf-payload-%03d", idx)

// Store the binary data
configData := map[string][]byte{
fileName: data,
}

spinner.Updatef("Adding archive binary configmap %d of %d to the cluster", idx+1, chunkCount)

// Attempt to create the configmap in the cluster
if _, err = c.ReplaceConfigmap(ctx, ZarfNamespaceName, fileName, configData); err != nil {
return configMaps, "", err
// TODO: Replace with create or update.
err := c.Clientset.CoreV1().ConfigMaps(ZarfNamespaceName).Delete(ctx, fileName, metav1.DeleteOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return nil, "", err
}
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: fileName,
Namespace: ZarfNamespaceName,
},
BinaryData: map[string][]byte{
fileName: data,
},
}
_, err = c.Clientset.CoreV1().ConfigMaps(ZarfNamespaceName).Create(ctx, cm, metav1.CreateOptions{})
if err != nil {
return nil, "", err
}

// Add the configmap to the configmaps slice for later usage in the pod
Expand Down Expand Up @@ -291,22 +312,30 @@ func (c *Cluster) injectorIsReady(ctx context.Context, seedImages []transform.Im
}

func (c *Cluster) createInjectorConfigMap(ctx context.Context, binaryPath string) error {
var err error
configData := make(map[string][]byte)

// Add the injector binary data to the configmap
if configData["zarf-injector"], err = os.ReadFile(binaryPath); err != nil {
name := "rust-binary"
// TODO: Replace with a create or update.
err := c.Clientset.CoreV1().ConfigMaps(ZarfNamespaceName).Delete(ctx, name, metav1.DeleteOptions{})
if err != nil && !kerrors.IsNotFound(err) {
return err
}

// Try to delete configmap silently
_ = c.DeleteConfigmap(ctx, ZarfNamespaceName, "rust-binary")

// Attempt to create the configmap in the cluster
if _, err = c.CreateConfigmap(ctx, ZarfNamespaceName, "rust-binary", configData); err != nil {
b, err := os.ReadFile(binaryPath)
if err != nil {
return err
}
configData := map[string][]byte{
"zarf-injector": b,
}
configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: ZarfNamespaceName,
},
BinaryData: configData,
}
_, err = c.Clientset.CoreV1().ConfigMaps(configMap.Namespace).Create(ctx, configMap, metav1.CreateOptions{})
if err != nil {
return err
}

return nil
}

Expand Down
62 changes: 0 additions & 62 deletions src/pkg/k8s/configmap.go

This file was deleted.

Loading