diff --git a/internal/controller/consts.go b/internal/controller/consts.go index 0f62ade62..e9ed5f119 100644 --- a/internal/controller/consts.go +++ b/internal/controller/consts.go @@ -22,9 +22,4 @@ const ( // preflightFailedRequeueAfter is how long to wait before trying to reconcile // if some preflight check has failed. preflightFailedRequeueAfter = 30 * time.Second - - httpsScheme = "https" - githubDomain = "github.com" - gitlabHostPrefix = "gitlab." - gitlabPackagesAPIPrefix = "/api/v4/projects/" ) diff --git a/internal/controller/manifests_downloader.go b/internal/controller/manifests_downloader.go index ff4392b7a..087ab9c64 100644 --- a/internal/controller/manifests_downloader.go +++ b/internal/controller/manifests_downloader.go @@ -26,10 +26,13 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" - operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2" + ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/reconcile" + + operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2" + "sigs.k8s.io/cluster-api-operator/util" ) const ( @@ -76,7 +79,7 @@ func (p *phaseReconciler) downloadManifests(ctx context.Context) (reconcile.Resu log.Info("Downloading provider manifests") - repo, err := repositoryFactory(p.providerConfig, p.configClient.Variables()) + repo, err := util.RepositoryFactory(p.providerConfig, p.configClient.Variables()) if err != nil { err = fmt.Errorf("failed to create repo from provider url for provider %q: %w", p.provider.GetName(), err) diff --git a/internal/controller/phases.go b/internal/controller/phases.go index 7729adfa5..8a4311b48 100644 --- a/internal/controller/phases.go +++ b/internal/controller/phases.go @@ -22,8 +22,6 @@ import ( "context" "fmt" "io" - "net/url" - "strings" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -528,42 +526,6 @@ func (p *phaseReconciler) newClusterClient() cluster.Client { })) } -// repositoryFactory returns the repository implementation corresponding to the provider URL. -// inspired by https://github.com/kubernetes-sigs/cluster-api/blob/124d9be7035e492f027cdc7a701b6b179451190a/cmd/clusterctl/client/repository/client.go#L170 -func repositoryFactory(providerConfig configclient.Provider, configVariablesClient configclient.VariablesClient) (repository.Repository, error) { - // parse the repository url - rURL, err := url.Parse(providerConfig.URL()) - if err != nil { - return nil, fmt.Errorf("failed to parse repository url %q", providerConfig.URL()) - } - - if rURL.Scheme != httpsScheme { - return nil, fmt.Errorf("invalid provider url. there are no provider implementation for %q schema", rURL.Scheme) - } - - // if the url is a GitHub repository - if rURL.Host == githubDomain { - repo, err := repository.NewGitHubRepository(providerConfig, configVariablesClient) - if err != nil { - return nil, fmt.Errorf("error creating the GitHub repository client: %w", err) - } - - return repo, err - } - - // if the url is a GitLab repository - if strings.HasPrefix(rURL.Host, gitlabHostPrefix) && strings.HasPrefix(rURL.Path, gitlabPackagesAPIPrefix) { - repo, err := repository.NewGitLabRepository(providerConfig, configVariablesClient) - if err != nil { - return nil, fmt.Errorf("error creating the GitLab repository client: %w", err) - } - - return repo, err - } - - return nil, fmt.Errorf("invalid provider url. Only GitHub and GitLab are supported for %q schema", rURL.Scheme) -} - func getLatestVersion(repoVersions []string) (string, error) { if len(repoVersions) == 0 { err := fmt.Errorf("no versions available") diff --git a/internal/controller/phases_test.go b/internal/controller/phases_test.go index d9d6a08b2..2025c1bff 100644 --- a/internal/controller/phases_test.go +++ b/internal/controller/phases_test.go @@ -30,6 +30,7 @@ import ( operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2" "sigs.k8s.io/cluster-api-operator/internal/controller/genericprovider" + "sigs.k8s.io/cluster-api-operator/util" ) func TestSecretReader(t *testing.T) { @@ -472,7 +473,7 @@ func TestRepositoryFactory(t *testing.T) { providerConfig, err := configClient.Providers().Get(providerName, providerType) g.Expect(err).ToNot(HaveOccurred()) - repo, err := repositoryFactory(providerConfig, configClient.Variables()) + repo, err := util.RepositoryFactory(providerConfig, configClient.Variables()) if tc.expectedError { g.Expect(err).To(HaveOccurred()) diff --git a/util/util.go b/util/util.go index c9aac381b..ffa6f5920 100644 --- a/util/util.go +++ b/util/util.go @@ -17,9 +17,22 @@ limitations under the License. package util import ( + "fmt" + "net/url" + "strings" + operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2" "sigs.k8s.io/cluster-api-operator/internal/controller/genericprovider" clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" + configclient "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config" + "sigs.k8s.io/cluster-api/cmd/clusterctl/client/repository" +) + +const ( + httpsScheme = "https" + githubDomain = "github.com" + gitlabHostPrefix = "gitlab." + gitlabPackagesAPIPrefix = "/api/v4/projects/" ) func IsCoreProvider(p genericprovider.GenericProvider) bool { @@ -44,3 +57,39 @@ func ClusterctlProviderType(genericProvider genericprovider.GenericProvider) clu return clusterctlv1.ProviderTypeUnknown } + +// RepositoryFactory returns the repository implementation corresponding to the provider URL. +// inspired by https://github.com/kubernetes-sigs/cluster-api/blob/124d9be7035e492f027cdc7a701b6b179451190a/cmd/clusterctl/client/repository/client.go#L170 +func RepositoryFactory(providerConfig configclient.Provider, configVariablesClient configclient.VariablesClient) (repository.Repository, error) { + // parse the repository url + rURL, err := url.Parse(providerConfig.URL()) + if err != nil { + return nil, fmt.Errorf("failed to parse repository url %q", providerConfig.URL()) + } + + if rURL.Scheme != httpsScheme { + return nil, fmt.Errorf("invalid provider url. there are no provider implementation for %q schema", rURL.Scheme) + } + + // if the url is a GitHub repository + if rURL.Host == githubDomain { + repo, err := repository.NewGitHubRepository(providerConfig, configVariablesClient) + if err != nil { + return nil, fmt.Errorf("error creating the GitHub repository client: %w", err) + } + + return repo, err + } + + // if the url is a GitLab repository + if strings.HasPrefix(rURL.Host, gitlabHostPrefix) && strings.HasPrefix(rURL.Path, gitlabPackagesAPIPrefix) { + repo, err := repository.NewGitLabRepository(providerConfig, configVariablesClient) + if err != nil { + return nil, fmt.Errorf("error creating the GitLab repository client: %w", err) + } + + return repo, err + } + + return nil, fmt.Errorf("invalid provider url. Only GitHub and GitLab are supported for %q schema", rURL.Scheme) +}