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

Add test for introducing a required config item in EC updates #4989

Closed
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
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ mock:
mockgen -source=pkg/store/store_interface.go -destination=pkg/store/mock/mock.go
mockgen -source=pkg/handlers/interface.go -destination=pkg/handlers/mock/mock.go
mockgen -source=pkg/operator/client/client_interface.go -destination=pkg/operator/client/mock/mock.go
mockgen -source=pkg/k8sutil/interface.go -destination=pkg/k8sutil/mock/mock.go
mockgen -source=pkg/pull/interface.go -destination=pkg/pull/mock/mock.go

.PHONY: dev
dev:
Expand Down
2 changes: 1 addition & 1 deletion pkg/automation/automation.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func AutomateInstall(opts AutomateInstallOptions) error {
return nil
}

func installLicenseSecret(clientset *kubernetes.Clientset, licenseSecret corev1.Secret, additionalFiles map[string][]byte) (finalError error) {
func installLicenseSecret(clientset kubernetes.Interface, licenseSecret corev1.Secret, additionalFiles map[string][]byte) (finalError error) {
license, ok := licenseSecret.Data["license"]
if !ok {
return fmt.Errorf("license secret %q does not contain a license field", licenseSecret.Name)
Expand Down
2 changes: 1 addition & 1 deletion pkg/docker/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func applicationPullSecretLabels() map[string]string {
return secretLabels
}

func EnsureDockerHubSecret(username string, password string, namespace string, clientset *kubernetes.Clientset) error {
func EnsureDockerHubSecret(username string, password string, namespace string, clientset kubernetes.Interface) error {
dockerHubSecretMutex.Lock()
defer dockerHubSecretMutex.Unlock()

Expand Down
6 changes: 5 additions & 1 deletion pkg/k8sutil/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

type k8sutil struct{}

var _ K8sutil = (*k8sutil)(nil)

const (
DEFAULT_K8S_CLIENT_QPS = 100
DEFAULT_K8S_CLIENT_BURST = 100
Expand All @@ -41,7 +45,7 @@ func AddFlags(flags *flag.FlagSet) {
kubernetesConfigFlags.AddFlags(flags)
}

func GetClientset() (*kubernetes.Clientset, error) {
func (k *k8sutil) GetClientset() (kubernetes.Interface, error) {
cfg, err := GetClusterConfig()
if err != nil {
return nil, errors.Wrap(err, "failed to get cluster config")
Expand Down
2 changes: 1 addition & 1 deletion pkg/k8sutil/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"k8s.io/client-go/kubernetes"
)

func GetCurrentRules(deployOptions kotsadmtypes.DeployOptions, clientset *kubernetes.Clientset) ([]rbacv1.PolicyRule, error) {
func GetCurrentRules(deployOptions kotsadmtypes.DeployOptions, clientset kubernetes.Interface) ([]rbacv1.PolicyRule, error) {
sar := &authorizationv1.SelfSubjectRulesReview{
Spec: authorizationv1.SelfSubjectRulesReviewSpec{
Namespace: deployOptions.Namespace,
Expand Down
4 changes: 2 additions & 2 deletions pkg/k8sutil/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func InitHelmCapabilities() error {
return errors.Wrap(err, "failed to create kubernetes clientset")
}

serverGroups, err := clientset.ServerGroups()
serverGroups, err := clientset.Discovery().ServerGroups()
if err != nil {
return errors.Wrap(err, "failed to get server groups")
}

serverVersion, err := clientset.ServerVersion()
serverVersion, err := clientset.Discovery().ServerVersion()
if err != nil {
return errors.Wrap(err, "failed to get server version")
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/k8sutil/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package k8sutil

import "k8s.io/client-go/kubernetes"

var _k8s K8sutil

func init() {
_k8s = &k8sutil{}
}

func Mock(k8s K8sutil) {
_k8s = k8s
}

type K8sutil interface {
GetClientset() (kubernetes.Interface, error)
}

// Convenience functions

func GetClientset() (kubernetes.Interface, error) {
return _k8s.GetClientset()
}
30 changes: 15 additions & 15 deletions pkg/k8sutil/kotsadm.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func UpdateKotsadmIDConfigMap(clientset kubernetes.Interface, kotsadmID string)
return nil
}

func FindKotsadm(clientset *kubernetes.Clientset, namespace string) (string, error) {
func FindKotsadm(clientset kubernetes.Interface, namespace string) (string, error) {
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: "app=kotsadm"})
if err != nil {
return "", errors.Wrap(err, "failed to list pods")
Expand Down Expand Up @@ -214,7 +214,7 @@ func WaitForKotsadm(clientset kubernetes.Interface, namespace string, timeoutWai
}
}

func RestartKotsadm(ctx context.Context, clientset *kubernetes.Clientset, namespace string, timeout time.Duration) error {
func RestartKotsadm(ctx context.Context, clientset kubernetes.Interface, namespace string, timeout time.Duration) error {
pods, err := clientset.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{LabelSelector: "app=kotsadm"})
if err != nil {
return errors.Wrap(err, "failed to list pods for termination")
Expand Down Expand Up @@ -261,7 +261,7 @@ func RestartKotsadm(ctx context.Context, clientset *kubernetes.Clientset, namesp
}
}

func DeleteKotsadm(ctx context.Context, clientset *kubernetes.Clientset, namespace string, isKurl bool) error {
func DeleteKotsadm(ctx context.Context, clientset kubernetes.Interface, namespace string, isKurl bool) error {
selectorLabels := map[string]string{
kotsadmtypes.KotsadmKey: kotsadmtypes.KotsadmLabelValue,
}
Expand Down Expand Up @@ -417,7 +417,7 @@ func DeleteKotsadm(ctx context.Context, clientset *kubernetes.Clientset, namespa
return nil
}

func waitForDeleteServices(ctx context.Context, clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
func waitForDeleteServices(ctx context.Context, clientset kubernetes.Interface, namespace string, listOptions metav1.ListOptions) error {
for {
services, err := clientset.CoreV1().Services(namespace).List(ctx, listOptions)
if err != nil {
Expand All @@ -430,7 +430,7 @@ func waitForDeleteServices(ctx context.Context, clientset *kubernetes.Clientset,
}
}

func waitForDeleteDeployments(ctx context.Context, clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
func waitForDeleteDeployments(ctx context.Context, clientset kubernetes.Interface, namespace string, listOptions metav1.ListOptions) error {
for {
deployments, err := clientset.AppsV1().Deployments(namespace).List(context.TODO(), listOptions)
if err != nil {
Expand All @@ -443,7 +443,7 @@ func waitForDeleteDeployments(ctx context.Context, clientset *kubernetes.Clients
}
}

func waitForDeleteStatefulSets(ctx context.Context, clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
func waitForDeleteStatefulSets(ctx context.Context, clientset kubernetes.Interface, namespace string, listOptions metav1.ListOptions) error {
for {
statefulsets, err := clientset.AppsV1().StatefulSets(namespace).List(context.TODO(), listOptions)
if err != nil {
Expand All @@ -456,7 +456,7 @@ func waitForDeleteStatefulSets(ctx context.Context, clientset *kubernetes.Client
}
}

func waitForDeletePods(ctx context.Context, clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
func waitForDeletePods(ctx context.Context, clientset kubernetes.Interface, namespace string, listOptions metav1.ListOptions) error {
for {
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), listOptions)
if err != nil {
Expand All @@ -469,7 +469,7 @@ func waitForDeletePods(ctx context.Context, clientset *kubernetes.Clientset, nam
}
}

func waitForDeletePVCs(ctx context.Context, clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
func waitForDeletePVCs(ctx context.Context, clientset kubernetes.Interface, namespace string, listOptions metav1.ListOptions) error {
for {
pvcs, err := clientset.CoreV1().PersistentVolumeClaims(namespace).List(context.TODO(), listOptions)
if err != nil {
Expand All @@ -482,7 +482,7 @@ func waitForDeletePVCs(ctx context.Context, clientset *kubernetes.Clientset, nam
}
}

func waitForDeleteSecrets(ctx context.Context, clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
func waitForDeleteSecrets(ctx context.Context, clientset kubernetes.Interface, namespace string, listOptions metav1.ListOptions) error {
for {
secrets, err := clientset.CoreV1().Secrets(namespace).List(context.TODO(), listOptions)
if err != nil {
Expand All @@ -495,7 +495,7 @@ func waitForDeleteSecrets(ctx context.Context, clientset *kubernetes.Clientset,
}
}

func waitForDeleteConfigmaps(ctx context.Context, clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
func waitForDeleteConfigmaps(ctx context.Context, clientset kubernetes.Interface, namespace string, listOptions metav1.ListOptions) error {
for {
configmaps, err := clientset.CoreV1().ConfigMaps(namespace).List(context.TODO(), listOptions)
if err != nil {
Expand All @@ -508,7 +508,7 @@ func waitForDeleteConfigmaps(ctx context.Context, clientset *kubernetes.Clientse
}
}

func waitForDeleteClusterRoleBindings(ctx context.Context, clientset *kubernetes.Clientset, listOptions metav1.ListOptions) error {
func waitForDeleteClusterRoleBindings(ctx context.Context, clientset kubernetes.Interface, listOptions metav1.ListOptions) error {
for {
crbs, err := clientset.RbacV1().ClusterRoleBindings().List(context.TODO(), listOptions)
if err != nil {
Expand All @@ -521,7 +521,7 @@ func waitForDeleteClusterRoleBindings(ctx context.Context, clientset *kubernetes
}
}

func waitForDeleteRoleBindings(ctx context.Context, clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
func waitForDeleteRoleBindings(ctx context.Context, clientset kubernetes.Interface, namespace string, listOptions metav1.ListOptions) error {
for {
rbs, err := clientset.RbacV1().RoleBindings(namespace).List(context.TODO(), listOptions)
if err != nil {
Expand All @@ -534,7 +534,7 @@ func waitForDeleteRoleBindings(ctx context.Context, clientset *kubernetes.Client
}
}

func waitForDeleteClusterRoles(ctx context.Context, clientset *kubernetes.Clientset, listOptions metav1.ListOptions) error {
func waitForDeleteClusterRoles(ctx context.Context, clientset kubernetes.Interface, listOptions metav1.ListOptions) error {
for {
crs, err := clientset.RbacV1().ClusterRoles().List(context.TODO(), listOptions)
if err != nil {
Expand All @@ -547,7 +547,7 @@ func waitForDeleteClusterRoles(ctx context.Context, clientset *kubernetes.Client
}
}

func waitForDeleteRoles(ctx context.Context, clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
func waitForDeleteRoles(ctx context.Context, clientset kubernetes.Interface, namespace string, listOptions metav1.ListOptions) error {
for {
roles, err := clientset.RbacV1().Roles(namespace).List(context.TODO(), listOptions)
if err != nil {
Expand All @@ -560,7 +560,7 @@ func waitForDeleteRoles(ctx context.Context, clientset *kubernetes.Clientset, na
}
}

func waitForDeleteServiceAccounts(ctx context.Context, clientset *kubernetes.Clientset, namespace string, listOptions metav1.ListOptions) error {
func waitForDeleteServiceAccounts(ctx context.Context, clientset kubernetes.Interface, namespace string, listOptions metav1.ListOptions) error {
for {
serviceAccounts, err := clientset.CoreV1().ServiceAccounts(namespace).List(context.TODO(), listOptions)
if err != nil {
Expand Down
50 changes: 50 additions & 0 deletions pkg/k8sutil/mock/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/k8sutil/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func createDialer(cfg *rest.Config, namespace string, podName string) (httpstrea
return spdy.NewDialer(upgrader, &http.Client{Transport: roundTripper}, http.MethodPost, &serverURL), nil
}

func ServiceForward(clientset *kubernetes.Clientset, cfg *rest.Config, localPort int, remotePort int, namespace string, serviceName string) (chan struct{}, error) {
func ServiceForward(clientset kubernetes.Interface, cfg *rest.Config, localPort int, remotePort int, namespace string, serviceName string) (chan struct{}, error) {
isPortAvailable, err := IsPortAvailable(clientset, localPort)
if err != nil {
return nil, errors.Wrap(err, "failed to check if port is available")
Expand Down Expand Up @@ -454,7 +454,7 @@ func ServiceForward(clientset *kubernetes.Clientset, cfg *rest.Config, localPort
return stopChan, nil
}

func getFirstPod(clientset *kubernetes.Clientset, namespace string, selector string) (string, error) {
func getFirstPod(clientset kubernetes.Interface, namespace string, selector string) (string, error) {
options := metav1.ListOptions{LabelSelector: selector}

podList, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), options)
Expand Down
6 changes: 3 additions & 3 deletions pkg/kotsadm/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// removeNodeAPI should be removable when we don't need to support direct upgrade paths from 1.19.6 and before
func removeNodeAPI(deployOptions *types.DeployOptions, clientset *kubernetes.Clientset) error {
func removeNodeAPI(deployOptions *types.DeployOptions, clientset kubernetes.Interface) error {
ns := deployOptions.Namespace

err := clientset.AppsV1().Deployments(ns).Delete(context.TODO(), "kotsadm-api", metav1.DeleteOptions{})
Expand All @@ -35,7 +35,7 @@ func removeNodeAPI(deployOptions *types.DeployOptions, clientset *kubernetes.Cli
}

// removeNodeAPIRBAC should be removable when we don't need to support direct upgrade paths from 1.19.6 and before
func removeNodeAPIRBAC(deployOptions *types.DeployOptions, clientset *kubernetes.Clientset) error {
func removeNodeAPIRBAC(deployOptions *types.DeployOptions, clientset kubernetes.Interface) error {
isClusterScoped, err := isKotsadmClusterScoped(deployOptions)
if err != nil {
return errors.Wrap(err, "failed to check if kotsadm api is cluster scoped")
Expand All @@ -60,7 +60,7 @@ func removeNodeAPIRBAC(deployOptions *types.DeployOptions, clientset *kubernetes
}

// removeNodeAPIClusterRBAC should be removable when we don't need to support direct upgrade paths from 1.19.6 and before
func removeNodeAPIClusterRBAC(deployOptions *types.DeployOptions, clientset *kubernetes.Clientset) error {
func removeNodeAPIClusterRBAC(deployOptions *types.DeployOptions, clientset kubernetes.Interface) error {
err := clientset.CoreV1().ServiceAccounts(deployOptions.Namespace).Delete(context.TODO(), "kotsadm-api", metav1.DeleteOptions{})
if err != nil && !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to delete api service account")
Expand Down
2 changes: 1 addition & 1 deletion pkg/kotsadm/application_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func getApplicationMetadataYAML(data []byte, namespace string, upstreamURI strin
return docs, nil
}

func ensureApplicationMetadata(deployOptions types.DeployOptions, clientset *kubernetes.Clientset) error {
func ensureApplicationMetadata(deployOptions types.DeployOptions, clientset kubernetes.Interface) error {
_, err := clientset.CoreV1().ConfigMaps(deployOptions.Namespace).Get(context.TODO(), "kotsadm-application-metadata", metav1.GetOptions{})
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/kotsadm/configmaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func getConfigMapsYAML(deployOptions types.DeployOptions) (map[string][]byte, er
return docs, nil
}

func ensureKotsadmConfig(deployOptions types.DeployOptions, clientset *kubernetes.Clientset) error {
func ensureKotsadmConfig(deployOptions types.DeployOptions, clientset kubernetes.Interface) error {
if err := kotsadmresources.EnsurePrivateKotsadmRegistrySecret(deployOptions.Namespace, deployOptions.RegistryConfig, clientset); err != nil {
return errors.Wrap(err, "failed to ensure private kotsadm registry secret")
}
Expand All @@ -45,7 +45,7 @@ func ensureKotsadmConfig(deployOptions types.DeployOptions, clientset *kubernete
return nil
}

func EnsureConfigMaps(deployOptions types.DeployOptions, clientset *kubernetes.Clientset) error {
func EnsureConfigMaps(deployOptions types.DeployOptions, clientset kubernetes.Interface) error {
desiredConfigMap := kotsadmobjects.KotsadmConfigMap(deployOptions)

existingConfigMap, err := clientset.CoreV1().ConfigMaps(deployOptions.Namespace).Get(context.TODO(), types.KotsadmConfigMap, metav1.GetOptions{})
Expand Down Expand Up @@ -77,7 +77,7 @@ func updateConfigMap(existingConfigMap, desiredConfigMap *corev1.ConfigMap) *cor
return existingConfigMap
}

func ensureWaitForAirgapConfig(deployOptions types.DeployOptions, clientset *kubernetes.Clientset, configMapName string) error {
func ensureWaitForAirgapConfig(deployOptions types.DeployOptions, clientset kubernetes.Interface, configMapName string) error {
additionalLabels := map[string]string{
"kots.io/automation": "airgap",
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func ensureWaitForAirgapConfig(deployOptions types.DeployOptions, clientset *kub
return nil
}

func ensureConfigMapWithData(deployOptions types.DeployOptions, clientset *kubernetes.Clientset, configMapName string, data map[string]string) error {
func ensureConfigMapWithData(deployOptions types.DeployOptions, clientset kubernetes.Interface, configMapName string, data map[string]string) error {
configMap, err := configMapWithData(deployOptions, configMapName, data)
if err != nil {
return errors.Wrap(err, "failed to build config map")
Expand Down
4 changes: 2 additions & 2 deletions pkg/kotsadm/configvalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"k8s.io/client-go/kubernetes/scheme"
)

func ensureConfigValuesSecret(deployOptions *types.DeployOptions, clientset *kubernetes.Clientset) (bool, error) {
func ensureConfigValuesSecret(deployOptions *types.DeployOptions, clientset kubernetes.Interface) (bool, error) {
existingSecret, err := getConfigValuesSecret(deployOptions.Namespace, clientset)
if err != nil {
return false, errors.Wrap(err, "failed to check for existing config values secret")
Expand All @@ -39,7 +39,7 @@ func ensureConfigValuesSecret(deployOptions *types.DeployOptions, clientset *kub
return true, nil
}

func getConfigValuesSecret(namespace string, clientset *kubernetes.Clientset) (*corev1.Secret, error) {
func getConfigValuesSecret(namespace string, clientset kubernetes.Interface) (*corev1.Secret, error) {
configValuesSecret, err := clientset.CoreV1().Secrets(namespace).Get(context.TODO(), "kotsadm-default-configvalues", metav1.GetOptions{})
if err != nil {
if kuberneteserrors.IsNotFound(err) {
Expand Down
Loading
Loading