Skip to content

Commit

Permalink
CAA: add support to look up imagePullSecrets for pods
Browse files Browse the repository at this point in the history
This PR initializes auth.json with the imagePullSecrets listed on the
pod and service account.

Fixes: #2231


Signed-off-by: Silenio Quarti <[email protected]>
  • Loading branch information
squarti committed Jan 9, 2025
1 parent 3dc40c0 commit 72ee02e
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/cloud-api-adaptor/install/rbac/peer-pod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "list"]
- apiGroups: [""]
resources: ["serviceaccounts"]
verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
Expand Down
11 changes: 11 additions & 0 deletions src/cloud-api-adaptor/pkg/adaptor/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,17 @@ func (s *cloudService) CreateVM(ctx context.Context, req *pb.CreateVMRequest) (r
_, err = os.Stat(SrcAuthfilePath)
if err != nil {
logger.Printf("credential file %s is not present, skipping image auth config", SrcAuthfilePath)

// Look up image pull secrets for the pod
authJSON, err = k8sops.GetImagePullSecrets(pod, namespace)
if err != nil {
// Ignore errors
logger.Printf("error reading image pull secrets: %v", err)
}
if authJSON != nil {
logger.Printf("successfully retrieved pod image pull secrets for %s/%s", namespace, pod)
authFilePath = AuthFilePath
}
} else {
authJSON, err = os.ReadFile(SrcAuthfilePath)
if err != nil {
Expand Down
91 changes: 91 additions & 0 deletions src/cloud-api-adaptor/pkg/adaptor/k8sops/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,97 @@ func RemoveExtendedResources() error {
return nil
}

// Auths contains Registries with credentials
type Auths struct {
Registries Registries `json:"auths"`
}

// Registries contains credentials for hosts
type Registries map[string]Auth

// Auth contains credentials for a given host
type Auth struct {
Auth string `json:"auth"`
}

// GetImagePullSecrets gets image pull secrets for the specified pod
func GetImagePullSecrets(podName string, namespace string) ([]byte, error) {

config, err := getKubeConfig()
if err != nil {
return nil, fmt.Errorf("failed to get k8s config: %v", err)
}

cli, err := getClient(config)
if err != nil {
return nil, fmt.Errorf("failed to get k8s client: %v", err)
}

pod, err := cli.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{})
if err != nil {
return nil, err
}

accountName := pod.Spec.ServiceAccountName
if accountName == "" {
accountName = "default"
}
serviceaAccount, err := cli.CoreV1().ServiceAccounts(namespace).Get(context.TODO(), accountName, metav1.GetOptions{})
if err != nil {
return nil, err
}

auths := Auths{}
auths.Registries = make(map[string]Auth)
for _, secret := range serviceaAccount.ImagePullSecrets {
err := getAuths(cli, namespace, secret.Name, &auths)
if err != nil {
return nil, err
}
}
for _, secret := range pod.Spec.ImagePullSecrets {
err := getAuths(cli, namespace, secret.Name, &auths)
if err != nil {
return nil, err
}
}

if len(auths.Registries) > 0 {
authJSON, err := json.Marshal(auths)
if err != nil {
return nil, err
}
return authJSON, nil
}
return nil, nil
}

// getAuths get auth credentials from specified docker secret
func getAuths(cli *k8sclient.Clientset, namespace string, secretName string, auths *Auths) error {
secret, err := cli.CoreV1().Secrets(namespace).Get(context.TODO(), secretName, metav1.GetOptions{})
if err != nil {
return err
}
registries := Registries{}
if secretData, ok := secret.Data[".dockerconfigjson"]; ok {
auths := Auths{}
err := json.Unmarshal(secretData, &auths)
if err != nil {
return err
}
registries = auths.Registries
} else if secretData, ok := secret.Data[".dockercfg"]; ok {
err = json.Unmarshal(secretData, &registries)
if err != nil {
return err
}
}
for registry, creds := range registries {
auths.Registries[registry] = creds
}
return nil
}

// patchNodeStatus patches the status of a node
func patchNodeStatus(c *k8sclient.Clientset, nodeName string, patches []jsonPatch) error {
if len(patches) > 0 {
Expand Down

0 comments on commit 72ee02e

Please sign in to comment.