Skip to content

Commit

Permalink
Move RepositoryFactory function to common utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedosin committed Jan 24, 2024
1 parent d6e5f4f commit 262d394
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 46 deletions.
7 changes: 2 additions & 5 deletions internal/controller/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ const (
// if some preflight check has failed.
preflightFailedRequeueAfter = 30 * time.Second

httpsScheme = "https"
githubDomain = "github.com"
gitlabHostPrefix = "gitlab."
gitlabPackagesAPIPrefix = "/api/v4/projects/"
configPath = "/config/clusterctl.yaml"
// configPath is the path to the clusterctl config file.
configPath = "/config/clusterctl.yaml"
)
7 changes: 5 additions & 2 deletions internal/controller/manifests_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -76,7 +79,7 @@ func (p *phaseReconciler) downloadManifests(ctx context.Context) (reconcile.Resu

log.Info("Downloading provider manifests")

repo, err := repositoryFactory(ctx, p.providerConfig, p.configClient.Variables())
repo, err := util.RepositoryFactory(ctx, 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)

Expand Down
38 changes: 0 additions & 38 deletions internal/controller/phases.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import (
"context"
"fmt"
"io"
"net/url"
"os"
"strings"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -554,42 +552,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(ctx context.Context, 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(ctx, 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")
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/phases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"

operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
"sigs.k8s.io/cluster-api-operator/util"
)

func TestSecretReader(t *testing.T) {
Expand Down Expand Up @@ -475,7 +476,7 @@ func TestRepositoryFactory(t *testing.T) {
providerConfig, err := configClient.Providers().Get(providerName, providerType)
g.Expect(err).ToNot(HaveOccurred())

repo, err := repositoryFactory(ctx, providerConfig, configClient.Variables())
repo, err := util.RepositoryFactory(ctx, providerConfig, configClient.Variables())
if tc.expectedError {
g.Expect(err).To(HaveOccurred())

Expand Down
50 changes: 50 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,23 @@ limitations under the License.
package util

import (
"context"
"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 {
Expand All @@ -46,3 +60,39 @@ func ClusterctlProviderType(genericProvider operatorv1.GenericProvider) clusterc

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(ctx context.Context, 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(ctx, 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)
}

0 comments on commit 262d394

Please sign in to comment.