Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
Use library interacting with flux CRs
Browse files Browse the repository at this point in the history
  • Loading branch information
surajssd committed Sep 9, 2021
1 parent 4e917be commit 01466d2
Show file tree
Hide file tree
Showing 80 changed files with 11,237 additions and 173 deletions.
18 changes: 8 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ go 1.15

require (
github.com/elazarl/goproxy/ext v0.0.0-20210801061803-8e322dfb79c4 // indirect
github.com/evanphx/json-patch v4.11.0+incompatible // indirect
github.com/fluxcd/helm-controller/api v0.11.2
github.com/fluxcd/source-controller/api v0.15.4
github.com/go-openapi/spec v0.19.7 // indirect
github.com/go-openapi/swag v0.19.8 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/google/go-cmp v0.5.5
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/hashicorp/go-version v1.2.0
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl/v2 v2.7.2
github.com/hpcloud/tail v1.0.0
github.com/imdario/mergo v0.3.12 // indirect
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
github.com/kinvolk/fluxlib v0.0.0-00010101000000-000000000000
github.com/linkerd/linkerd2 v0.5.1-0.20210517230931-5535e9c4edda
github.com/mitchellh/go-homedir v1.1.0
github.com/onsi/ginkgo v1.16.4 // indirect
github.com/onsi/gomega v1.13.0 // indirect
github.com/packethost/packngo v0.2.0
github.com/prometheus/alertmanager v0.20.0
github.com/prometheus/client_golang v1.11.0
Expand All @@ -29,20 +28,19 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.0
github.com/zclconf/go-cty v1.7.0
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
helm.sh/helm/v3 v3.6.3
k8s.io/api v0.21.4
k8s.io/apiextensions-apiserver v0.21.4 // indirect
k8s.io/apiextensions-apiserver v0.21.4
k8s.io/apimachinery v0.21.4
k8s.io/client-go v0.21.4
k8s.io/utils v0.0.0-20210527160623-6fdb442a123b // indirect
sigs.k8s.io/yaml v1.2.0
)

// There is a big confusion how to use Docker in go modules. This points to v19.03.5.
replace github.com/docker/docker => github.com/docker/engine v1.4.2-0.20191113042239-ea84732a7725

replace github.com/kinvolk/fluxlib => /home/hummer/work/flux-libs

// With v0.2.0 package has been renames, so until all dependencies are updated to use new import name,
// we need to use older version.
//
Expand Down
60 changes: 56 additions & 4 deletions go.sum

Large diffs are not rendered by default.

96 changes: 18 additions & 78 deletions pkg/components/util/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,24 @@ import (
"fmt"
"time"

helmreleaseapi "github.com/fluxcd/helm-controller/api/v2beta1"
sourceapi "github.com/fluxcd/source-controller/api/v1beta1"
helmrelease "github.com/kinvolk/fluxlib/lib/helm-release"
sourcecontroller "github.com/kinvolk/fluxlib/lib/source-controller"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/kube"
"helm.sh/helm/v3/pkg/storage/driver"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kinvolk/lokomotive/internal"
"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/k8sutil"
"github.com/kinvolk/lokomotive/pkg/version"
)

var fluxInstallInterval = metav1.Duration{Duration: 5 * time.Minute} //nolint:gomnd

func generateGitRepository() *sourceapi.GitRepository {
// TODO: Figure out a way to distinguish between the release and the local development and the
// master branch.
Expand All @@ -47,7 +46,7 @@ func generateGitRepository() *sourceapi.GitRepository {
Namespace: "flux-system",
},
Spec: sourceapi.GitRepositorySpec{
Interval: metav1.Duration{5 * time.Minute},
Interval: fluxInstallInterval,
Reference: &sourceapi.GitRepositoryRef{
Commit: version.Commit,
Branch: version.Branch,
Expand All @@ -57,6 +56,8 @@ func generateGitRepository() *sourceapi.GitRepository {
}
}

// ExperimentalInstallComponent installs the given component on the cluster mentioned in the given
// kubeconfig by creating HelmRelease and GitRepository.
func ExperimentalInstallComponent(c components.Component, kubeconfig []byte) error {
hr, err := c.GenerateHelmRelease()
if err != nil {
Expand All @@ -65,92 +66,31 @@ func ExperimentalInstallComponent(c components.Component, kubeconfig []byte) err

gr := generateGitRepository()

clientConfig, err := clientcmd.NewClientConfigFromBytes(kubeconfig)
grc, err := sourcecontroller.NewGitRepoConfig(
sourcecontroller.WithKubeconfig(kubeconfig),
)
if err != nil {
return fmt.Errorf("creating client config failed: %w", err)
return fmt.Errorf("initializing GitRepository client: %w", err)
}

restConfig, err := clientConfig.ClientConfig()
if err != nil {
return fmt.Errorf("converting client config to rest client config failed: %w", err)
if err := grc.CreateOrUpdate(gr); err != nil {
return fmt.Errorf("creating/updating GitRepository: %w", err)
}

scheme := runtime.NewScheme()
_ = sourceapi.AddToScheme(scheme)
_ = helmreleaseapi.AddToScheme(scheme)

kclient, err := client.New(restConfig, client.Options{
Scheme: scheme,
})
hrc, err := helmrelease.NewHelmReleaseConfig(
helmrelease.WithKubeconfig(kubeconfig),
)
if err != nil {
return fmt.Errorf("creating kubernetes client: %w", err)
return fmt.Errorf("initializing HelmRelease client: %w", err)
}

if err := createOrUpdateGitRepository(kclient, gr); err != nil {
return fmt.Errorf("creating/updating GitRepository: %w", err)
}

if err := createOrUpdateHelmRelease(kclient, hr); err != nil {
if err := hrc.CreateOrUpdate(hr); err != nil {
return fmt.Errorf("creating/updating HelmRelease: %w", err)
}

return nil
}

func createOrUpdateHelmRelease(c client.Client, hr *helmreleaseapi.HelmRelease) error {
var got helmreleaseapi.HelmRelease
if err := c.Get(context.Background(), types.NamespacedName{
Namespace: hr.GetNamespace(),
Name: hr.GetName(),
}, &got); err != nil {
if errors.IsNotFound(err) {
// Create the object since it does not exists.
if err := c.Create(context.Background(), hr); err != nil {
return fmt.Errorf("creating HelmRelease: %w", err)
}

return nil
}

return fmt.Errorf("looking up HelmRelease: %w", err)
}

hr.ResourceVersion = got.ResourceVersion

if err := c.Update(context.Background(), hr); err != nil {
return fmt.Errorf("updating HelmRelease: %w", err)
}

return nil
}

func createOrUpdateGitRepository(c client.Client, gr *sourceapi.GitRepository) error {
var got sourceapi.GitRepository
if err := c.Get(context.Background(), types.NamespacedName{
Namespace: gr.GetNamespace(),
Name: gr.GetName(),
}, &got); err != nil {
if errors.IsNotFound(err) {
// Create the object since it does not exists.
if err := c.Create(context.Background(), gr); err != nil {
return fmt.Errorf("creating GitReposirtory: %w", err)
}

return nil
}

return fmt.Errorf("looking up GitRepository: %w", err)
}

gr.ResourceVersion = got.ResourceVersion

if err := c.Update(context.Background(), gr); err != nil {
return fmt.Errorf("updating GitRepository: %w", err)
}

return nil
}

// InstallComponent installs given component using given kubeconfig as a Helm release using a Helm client.
// Before installing the component, the release namespace is created/updated.
func InstallComponent(c components.Component, kubeconfig []byte) error {
Expand Down
Loading

0 comments on commit 01466d2

Please sign in to comment.