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

feat: add support for non-anonymous login for Network Isolated Clusters #5650

Merged
merged 64 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
80c9686
figuring out what broke
AlisonB319 Jan 21, 2025
6c9f547
add unit tests
AlisonB319 Jan 23, 2025
737d623
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Jan 23, 2025
1c265a6
small fixes and test data
AlisonB319 Jan 23, 2025
128af21
oras login tweak
AlisonB319 Jan 23, 2025
7ecf991
testdata
AlisonB319 Jan 23, 2025
8f68ae3
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Jan 23, 2025
c546369
spelling mistake
AlisonB319 Jan 23, 2025
7c03c1a
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Jan 23, 2025
95edfaa
working but broke unit tests and busybox pod can't pull the image
AlisonB319 Jan 24, 2025
0455e76
Merge branch 'alburgess/oras-identity2' of https://github.com/Azure/A…
AlisonB319 Jan 24, 2025
6f7a91a
testdata
AlisonB319 Jan 24, 2025
e48a288
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Jan 24, 2025
1ab8dae
testdata
AlisonB319 Jan 27, 2025
beb4fd9
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Jan 27, 2025
32a187e
working e2e test
AlisonB319 Jan 28, 2025
cbc2718
make variables less confusing
AlisonB319 Jan 28, 2025
cbf8aff
cleanup
AlisonB319 Jan 28, 2025
bce76d6
fix wrapping
AlisonB319 Jan 28, 2025
0b6d29b
fix name conflict for test
AlisonB319 Jan 28, 2025
b79deb2
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Jan 28, 2025
7fd3a3a
remove unneeded permissions identity
AlisonB319 Jan 28, 2025
a2deb50
Merge branch 'alburgess/oras-identity2' of https://github.com/Azure/A…
AlisonB319 Jan 28, 2025
754f695
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Jan 28, 2025
747d841
refactor without looking at a test image
AlisonB319 Jan 28, 2025
56a13e7
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Jan 28, 2025
e845a6b
spellcheck
AlisonB319 Jan 29, 2025
e96ca04
Merge branch 'alburgess/oras-identity2' of https://github.com/Azure/A…
AlisonB319 Jan 29, 2025
1ca51b4
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Jan 29, 2025
a7a4c06
fix cse and unit tests
AlisonB319 Jan 29, 2025
597d222
testdata
AlisonB319 Jan 29, 2025
290a4dd
add retries for tokens
AlisonB319 Jan 30, 2025
a49659d
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Jan 30, 2025
ebe5a4f
testdata
AlisonB319 Jan 30, 2025
2897924
fix syntax
AlisonB319 Jan 30, 2025
cb569df
testdata
AlisonB319 Jan 30, 2025
110ce21
fix conifg, testdata, check unit test logic
AlisonB319 Jan 30, 2025
2499a25
changes
AlisonB319 Jan 30, 2025
04dc972
fix config names
AlisonB319 Jan 30, 2025
ed48f1a
fix e2e vars
AlisonB319 Jan 30, 2025
47376b7
return the correct var
AlisonB319 Jan 30, 2025
19053d6
fix the var again
AlisonB319 Jan 30, 2025
2caf4fc
fixing for e2e pipeline
AlisonB319 Jan 30, 2025
2067b45
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Jan 30, 2025
1116bd6
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Jan 30, 2025
0713b0a
add comments back
AlisonB319 Jan 30, 2025
ab178dc
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Jan 31, 2025
3ea476f
spelling!
AlisonB319 Jan 31, 2025
25b0d46
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Jan 31, 2025
192c513
save
AlisonB319 Feb 4, 2025
4e429ea
merge master
AlisonB319 Feb 5, 2025
3cbb618
oras discover check
AlisonB319 Feb 6, 2025
52150da
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Feb 6, 2025
397de9e
touch ups
AlisonB319 Feb 6, 2025
8e228e1
remove comment
AlisonB319 Feb 6, 2025
6a01359
comment changes
AlisonB319 Feb 7, 2025
be447bf
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Feb 7, 2025
9c875dc
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Feb 7, 2025
3b9e1a9
e2e fixes
AlisonB319 Feb 7, 2025
2d4084b
use ls
AlisonB319 Feb 7, 2025
153ec0e
final tweak and testdata
AlisonB319 Feb 7, 2025
5c53d08
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Feb 7, 2025
788c62f
testdata
AlisonB319 Feb 8, 2025
6ca5916
Merge branch 'master' into alburgess/oras-identity2
AlisonB319 Feb 8, 2025
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
134 changes: 122 additions & 12 deletions e2e/aks_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package e2e

import (
"context"
"encoding/base64"
"errors"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"testing"

Expand All @@ -15,6 +18,11 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v6"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/privatedns/armprivatedns"
v1 "k8s.io/api/core/v1"
errorsk8s "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

func getKubenetClusterModel(name string) *armcontainerservice.ManagedCluster {
Expand Down Expand Up @@ -66,7 +74,7 @@ func getBaseClusterModel(clusterName string) *armcontainerservice.ManagedCluster
}
}

func addAirgapNetworkSettings(ctx context.Context, t *testing.T, clusterModel *armcontainerservice.ManagedCluster) error {
func addAirgapNetworkSettings(ctx context.Context, t *testing.T, clusterModel *armcontainerservice.ManagedCluster, privateACRName string) error {
t.Logf("Adding network settings for airgap cluster %s in rg %s", *clusterModel.Name, *clusterModel.Properties.NodeResourceGroup)

vnet, err := getClusterVNet(ctx, *clusterModel.Properties.NodeResourceGroup)
Expand Down Expand Up @@ -98,7 +106,7 @@ func addAirgapNetworkSettings(ctx context.Context, t *testing.T, clusterModel *a
return err
}

err = addPrivateEndpointForACR(ctx, t, *clusterModel.Properties.NodeResourceGroup, vnet)
err = addPrivateEndpointForACR(ctx, t, *clusterModel.Properties.NodeResourceGroup, privateACRName, vnet)
if err != nil {
return err
}
Expand Down Expand Up @@ -150,7 +158,7 @@ func airGapSecurityGroup(location, clusterFQDN string) (armnetwork.SecurityGroup
}, nil
}

func addPrivateEndpointForACR(ctx context.Context, t *testing.T, nodeResourceGroup string, vnet VNet) error {
func addPrivateEndpointForACR(ctx context.Context, t *testing.T, nodeResourceGroup, privateACRName string, vnet VNet) error {
t.Logf("Checking if private endpoint for private container registry is in rg %s", nodeResourceGroup)

var err error
Expand All @@ -165,7 +173,7 @@ func addPrivateEndpointForACR(ctx context.Context, t *testing.T, nodeResourceGro
}

var peResp armnetwork.PrivateEndpointsClientCreateOrUpdateResponse
if peResp, err = createPrivateEndpoint(ctx, t, nodeResourceGroup, privateEndpointName, config.PrivateACRName, vnet); err != nil {
if peResp, err = createPrivateEndpoint(ctx, t, nodeResourceGroup, privateEndpointName, privateACRName, vnet); err != nil {
return err
}

Expand Down Expand Up @@ -201,8 +209,8 @@ func privateEndpointExists(ctx context.Context, t *testing.T, nodeResourceGroup,
return false, nil
}

func createPrivateAzureContainerRegistry(ctx context.Context, t *testing.T, cluster *armcontainerservice.ManagedCluster, resourceGroup, privateACRName string) error {
t.Logf("Creating private Azure Container Registry in rg %s", resourceGroup)
func createPrivateAzureContainerRegistry(ctx context.Context, t *testing.T, cluster *armcontainerservice.ManagedCluster, resourceGroup, privateACRName string, isNonAnonymousPull bool) error {
t.Logf("Creating private Azure Container Registry %s in rg %s", privateACRName, resourceGroup)

acr, err := config.Azure.RegistriesClient.Get(ctx, resourceGroup, privateACRName, nil)
if err == nil {
Expand Down Expand Up @@ -238,8 +246,8 @@ func createPrivateAzureContainerRegistry(ctx context.Context, t *testing.T, clus
Name: to.Ptr(armcontainerregistry.SKUNamePremium),
},
Properties: &armcontainerregistry.RegistryProperties{
AdminUserEnabled: to.Ptr(false),
AnonymousPullEnabled: to.Ptr(true), // required to pull images from the private ACR without authentication
AdminUserEnabled: to.Ptr(isNonAnonymousPull), // if non-anonymous pull is enabled, admin user must be enabled to be able to set credentials for the debug pods
AnonymousPullEnabled: to.Ptr(!isNonAnonymousPull), // required to pull images from the private ACR without authentication
},
}
pollerResp, err := config.Azure.RegistriesClient.BeginCreate(
Expand All @@ -258,9 +266,106 @@ func createPrivateAzureContainerRegistry(ctx context.Context, t *testing.T, clus
}

t.Logf("Private Azure Container Registry created")
if err := addCacheRuelsToPrivateAzureContainerRegistry(ctx, t, config.ResourceGroupName, config.PrivateACRName); err != nil {
AlisonB319 marked this conversation as resolved.
Show resolved Hide resolved

if isNonAnonymousPull {
t.Logf("Creating the secret for non-anonymous pull ACR for the e2e debug pods")
kubeconfigPath := os.Getenv("HOME") + "/.kube/config"
if err := fetchAndSaveKubeconfig(ctx, t, resourceGroup, *cluster.Name, kubeconfigPath); err != nil {
t.Logf("failed to fetch kubeconfig: %v", err)
return err
}
username, password, err := getAzureContainerRegistryCredentials(ctx, t, resourceGroup, privateACRName)
if err != nil {
t.Logf("failed to get private ACR credentials: %v", err)
return err
}
if err := createKubernetesSecret(ctx, t, "default", kubeconfigPath, config.Config.ACRSecretName, privateACRName, username, password); err != nil {
t.Logf("failed to create Kubernetes secret: %v", err)
return err
}
}

if err := addCacheRulesToPrivateAzureContainerRegistry(ctx, t, config.ResourceGroupName, privateACRName); err != nil {
return fmt.Errorf("failed to add cache rules to private acr: %w", err)
}

return nil
}

func createKubernetesSecret(ctx context.Context, t *testing.T, namespace, kubeconfigPath, secretName, registryName, username, password string) error {
t.Logf("Creating Kubernetes secret %s in namespace %s", secretName, namespace)
config, err := clientcmd.BuildConfigFromFlags("", kubeconfigPath)
AlisonB319 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
t.Logf("failed to build Kubernetes config: %v", err)
return err
}

clientset, err := kubernetes.NewForConfig(config)
if err != nil {
t.Logf("failed to create Kubernetes client: %v", err)
return err
}

auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
dockerConfigJSON := fmt.Sprintf(`{
"auths": {
"%s.azurecr.io": {
"username": "%s",
"password": "%s",
"auth": "%s"
}
}
}`, registryName, username, password, auth)

secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: namespace,
},
Type: v1.SecretTypeDockerConfigJson,
Data: map[string][]byte{
v1.DockerConfigJsonKey: []byte(dockerConfigJSON),
},
}
_, err = clientset.CoreV1().Secrets(namespace).Create(ctx, secret, metav1.CreateOptions{})
if err != nil {
if !errorsk8s.IsAlreadyExists(err) {
t.Logf("failed to create Kubernetes secret: %v", err)
return err
}
}
t.Logf("Kubernetes secret created")
AlisonB319 marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

func getAzureContainerRegistryCredentials(ctx context.Context, t *testing.T, resourceGroup, privateACRName string) (string, string, error) {
t.Logf("Getting credentials for private Azure Container Registry in rg %s", resourceGroup)
acrCreds, err := config.Azure.RegistriesClient.ListCredentials(ctx, resourceGroup, privateACRName, nil)
if err != nil {
return "", "", fmt.Errorf("failed to get private ACR credentials: %w", err)
}
username := *acrCreds.Username
password := *acrCreds.Passwords[0].Value
t.Logf("Private Azure Container Registry credentials retrieved")
return username, password, nil
}

func fetchAndSaveKubeconfig(ctx context.Context, t *testing.T, resourceGroup, clusterName, kubeconfigPath string) error {
adminCredentials, err := config.Azure.AKS.ListClusterAdminCredentials(ctx, resourceGroup, clusterName, nil)
if err != nil {
return fmt.Errorf("failed to get cluster admin credentials: %w", err)
}
if len(adminCredentials.Kubeconfigs) == 0 {
return fmt.Errorf("no kubeconfig returned for cluster %s", clusterName)
}

if err := os.MkdirAll(filepath.Dir(kubeconfigPath), 0700); err != nil {
return fmt.Errorf("failed to create kubeconfig directory: %w", err)
}
if err := os.WriteFile(kubeconfigPath, adminCredentials.Kubeconfigs[0].Value, 0600); err != nil {
return fmt.Errorf("failed to save kubeconfig to %s: %w", kubeconfigPath, err)
}
t.Logf("Kubeconfig successfully saved to %s", kubeconfigPath)
return nil
}

Expand All @@ -285,6 +390,11 @@ func shouldRecreateACR(ctx context.Context, t *testing.T, resourceGroup, private

cacheRules, err := config.Azure.CacheRulesClient.Get(ctx, resourceGroup, privateACRName, "aks-managed-rule", nil)
if err != nil {
var azErr *azcore.ResponseError
if errors.As(err, &azErr) && azErr.StatusCode == 404 {
t.Logf("Private ACR cache not found, need to recreate")
return nil, true
}
return fmt.Errorf("failed to get cache rules: %w", err), false
}
if cacheRules.Properties != nil && cacheRules.Properties.TargetRepository != nil && *cacheRules.Properties.TargetRepository != config.Config.AzureContainerRegistrytargetRepository {
Expand All @@ -295,7 +405,7 @@ func shouldRecreateACR(ctx context.Context, t *testing.T, resourceGroup, private
return nil, false
}

func addCacheRuelsToPrivateAzureContainerRegistry(ctx context.Context, t *testing.T, resourceGroup, privateACRName string) error {
func addCacheRulesToPrivateAzureContainerRegistry(ctx context.Context, t *testing.T, resourceGroup, privateACRName string) error {
t.Logf("Adding cache rules to private Azure Container Registry in rg %s", resourceGroup)

cacheParams := armcontainerregistry.CacheRule{
Expand Down Expand Up @@ -324,9 +434,9 @@ func addCacheRuelsToPrivateAzureContainerRegistry(ctx context.Context, t *testin
return nil
}

func createPrivateEndpoint(ctx context.Context, t *testing.T, nodeResourceGroup, privateEndpointName, acrName string, vnet VNet) (armnetwork.PrivateEndpointsClientCreateOrUpdateResponse, error) {
func createPrivateEndpoint(ctx context.Context, t *testing.T, nodeResourceGroup, privateEndpointName, privateACRName string, vnet VNet) (armnetwork.PrivateEndpointsClientCreateOrUpdateResponse, error) {
t.Logf("Creating Private Endpoint in rg %s", nodeResourceGroup)
acrID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ContainerRegistry/registries/%s", config.Config.SubscriptionID, config.ResourceGroupName, acrName)
acrID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ContainerRegistry/registries/%s", config.Config.SubscriptionID, config.ResourceGroupName, privateACRName)

peParams := armnetwork.PrivateEndpoint{
Location: to.Ptr(config.Config.Location),
Expand Down
89 changes: 71 additions & 18 deletions e2e/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,37 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
"testing"
"time"

"github.com/Azure/agentbaker/e2e/config"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v2"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v6"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice/v6"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/google/uuid"
"k8s.io/apimachinery/pkg/util/wait"
)

var (
clusterKubenet *Cluster
clusterKubenetAirgap *Cluster
clusterAzureNetwork *Cluster

clusterKubenetError error
clusterKubenetAirgapError error
clusterAzureNetworkError error

clusterKubenetOnce sync.Once
clusterKubenetAirgapOnce sync.Once
clusterAzureNetworkOnce sync.Once
clusterKubenet *Cluster
clusterKubenetAirgap *Cluster
clusterKubenetNonAnonAirgap *Cluster
clusterAzureNetwork *Cluster

clusterKubenetError error
clusterKubenetAirgapError error
clusterKubenetNonAnonAirgapError error
clusterAzureNetworkError error

clusterKubenetOnce sync.Once
clusterKubenetAirgapOnce sync.Once
clusterKubenetNonAnonAirgapOnce sync.Once
clusterAzureNetworkOnce sync.Once
)

type Cluster struct {
Expand Down Expand Up @@ -62,26 +68,33 @@ func (c *Cluster) MaxPodsPerNode() (int, error) {
// sync.Once is used to ensure that only one cluster for the set of tests is created
func ClusterKubenet(ctx context.Context, t *testing.T) (*Cluster, error) {
clusterKubenetOnce.Do(func() {
clusterKubenet, clusterKubenetError = prepareCluster(ctx, t, getKubenetClusterModel("abe2e-kubenet"), false)
clusterKubenet, clusterKubenetError = prepareCluster(ctx, t, getKubenetClusterModel("abe2e-kubenet"), false, false)
})
return clusterKubenet, clusterKubenetError
}

func ClusterKubenetAirgap(ctx context.Context, t *testing.T) (*Cluster, error) {
clusterKubenetAirgapOnce.Do(func() {
clusterKubenetAirgap, clusterKubenetAirgapError = prepareCluster(ctx, t, getKubenetClusterModel("abe2e-kubenet-airgap-dev"), true) // TODO (alburgess): remove -dev once CCOA is over
clusterKubenetAirgap, clusterKubenetAirgapError = prepareCluster(ctx, t, getKubenetClusterModel("abe2e-kubenet-airgap"), true, false)
})
return clusterKubenetAirgap, clusterKubenetAirgapError
}

func ClusterKubenetAirgapNonAnon(ctx context.Context, t *testing.T) (*Cluster, error) {
clusterKubenetNonAnonAirgapOnce.Do(func() {
clusterKubenetNonAnonAirgap, clusterKubenetNonAnonAirgapError = prepareCluster(ctx, t, getKubenetClusterModel("abe2e-kubenet-nonanonpull-airgap"), true, true)
})
return clusterKubenetNonAnonAirgap, clusterKubenetNonAnonAirgapError
}

func ClusterAzureNetwork(ctx context.Context, t *testing.T) (*Cluster, error) {
clusterAzureNetworkOnce.Do(func() {
clusterAzureNetwork, clusterAzureNetworkError = prepareCluster(ctx, t, getAzureNetworkClusterModel("abe2e-azure-network"), false)
clusterAzureNetwork, clusterAzureNetworkError = prepareCluster(ctx, t, getAzureNetworkClusterModel("abe2e-azure-network"), false, false)
})
return clusterAzureNetwork, clusterAzureNetworkError
}

func prepareCluster(ctx context.Context, t *testing.T, cluster *armcontainerservice.ManagedCluster, isAirgap bool) (*Cluster, error) {
func prepareCluster(ctx context.Context, t *testing.T, cluster *armcontainerservice.ManagedCluster, isAirgap, isNonAnonymousPull bool) (*Cluster, error) {
ctx, cancel := context.WithTimeout(ctx, config.Config.TestTimeoutCluster)
defer cancel()
cluster.Name = to.Ptr(fmt.Sprintf("%s-%s", *cluster.Name, hash(cluster)))
Expand All @@ -101,13 +114,18 @@ func prepareCluster(ctx context.Context, t *testing.T, cluster *armcontainerserv
return nil, fmt.Errorf("get cluster subnet: %w", err)
}

privateACRName := config.PrivateACRName
if isNonAnonymousPull {
privateACRName = config.PrivateACRNameNotAnon
}
t.Logf("using private acr %q isAnonyomusPull %v", privateACRName, isNonAnonymousPull)
if isAirgap {
// private acr must be created before we add the debug daemonsets
if err := createPrivateAzureContainerRegistry(ctx, t, cluster, config.ResourceGroupName, config.PrivateACRName); err != nil {
if err := createPrivateAzureContainerRegistry(ctx, t, cluster, config.ResourceGroupName, privateACRName, isNonAnonymousPull); err != nil {
return nil, fmt.Errorf("failed to create private acr: %w", err)
}

if err := addAirgapNetworkSettings(ctx, t, cluster); err != nil {
if err := addAirgapNetworkSettings(ctx, t, cluster, privateACRName); err != nil {
return nil, fmt.Errorf("add airgap network settings: %w", err)
}
}
Expand All @@ -117,7 +135,17 @@ func prepareCluster(ctx context.Context, t *testing.T, cluster *armcontainerserv
return nil, fmt.Errorf("get kube client using cluster %q: %w", *cluster.Name, err)
}

if err := kube.EnsureDebugDaemonsets(ctx, t, isAirgap); err != nil {
if isNonAnonymousPull {
identity, err := config.Azure.UserAssignedIdentities.Get(ctx, config.ResourceGroupName, config.VMIdentityName, nil)
if err != nil {
t.Fatalf("failed to get VM identity: %v", err)
}
if err := assignACRPullToIdentity(ctx, t, privateACRName, *identity.Properties.PrincipalID); err != nil {
return nil, fmt.Errorf("assign acr pull to the managed identity: %w", err)
}
}

if err := kube.EnsureDebugDaemonsets(ctx, t, isAirgap, privateACRName); err != nil {
return nil, fmt.Errorf("ensure debug daemonsets for %q: %w", *cluster.Name, err)
}

Expand All @@ -136,6 +164,31 @@ func prepareCluster(ctx context.Context, t *testing.T, cluster *armcontainerserv
}, nil
}

func assignACRPullToIdentity(ctx context.Context, t *testing.T, privateACRName, principalID string) error {
t.Logf("assigning ACR-Pull role to %s", principalID)
scope := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ContainerRegistry/registries/%s", config.Config.SubscriptionID, config.ResourceGroupName, privateACRName)

uid := uuid.New().String()
_, err := config.Azure.RoleAssignments.Create(ctx, scope, uid, armauthorization.RoleAssignmentCreateParameters{
Properties: &armauthorization.RoleAssignmentProperties{
PrincipalID: to.Ptr(principalID),
// ACR-Pull role definition ID
RoleDefinitionID: to.Ptr("/providers/Microsoft.Authorization/roleDefinitions/7f951dda-4ed3-4680-a7ca-43fe172d538d"),
PrincipalType: to.Ptr(armauthorization.PrincipalTypeServicePrincipal),
},
}, nil)
var respError *azcore.ResponseError
if err != nil {
// if the role assignment already exists, ignore the error
if errors.As(err, &respError) && respError.StatusCode == http.StatusConflict {
return nil
}
t.Logf("failed to assign ACR-Pull role to identity %s, error: %v", config.VMIdentityName, err)
return err
}
return nil
}

func hash(cluster *armcontainerservice.ManagedCluster) string {
jsonData, err := json.Marshal(cluster)
if err != nil {
Expand Down
Loading
Loading