Skip to content

Commit

Permalink
Rewrite apply
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Jun 24, 2022
1 parent 787841c commit 1e8825c
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 29 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ replace (
require (
cuelang.org/go v0.4.3
github.com/AlecAivazis/survey/v2 v2.3.5
github.com/acorn-io/baaah v0.0.0-20220621163714-34375f25ecdb
github.com/acorn-io/baaah v0.0.0-20220624052343-a3d0714dcd95
github.com/containerd/console v1.0.3
github.com/containerd/containerd v1.6.6
github.com/google/go-containerregistry v0.10.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ github.com/acorn-io/apiserver-1 v0.0.0-20220608053213-0ffc3be57697 h1:zEzrL1ewSm
github.com/acorn-io/apiserver-1 v0.0.0-20220608053213-0ffc3be57697/go.mod h1:sG6OmZ4yEWeQ9JmGjnp8WgQAk9D9z4hivMFsUUh9QF8=
github.com/acorn-io/baaah v0.0.0-20220621163714-34375f25ecdb h1:AK/vua0bYfO6+E0jlVI+XhWNrgEzs+dfRBunpaoAotI=
github.com/acorn-io/baaah v0.0.0-20220621163714-34375f25ecdb/go.mod h1:9FvNcgPq6o0x8Z8Nfq9EvG6tTvw7yVkagoNcMjvcm10=
github.com/acorn-io/baaah v0.0.0-20220624052343-a3d0714dcd95 h1:ltqQq25iSE5//wiO6ohFKhp4fVxv6H7JyCzxIff/OcI=
github.com/acorn-io/baaah v0.0.0-20220624052343-a3d0714dcd95/go.mod h1:CSj9RfR9Ab5LsLmdAcJlrMtyz0tD1kfqAKiLnbkXDg0=
github.com/acorn-io/component-base v0.24.1-ot-1 h1:GRbiCcCxZdKovA1L8eLFIvI9pJp3kJFOtKTOW7LSsQI=
github.com/acorn-io/component-base v0.24.1-ot-1/go.mod h1:GLGWZB2NbdO0JXuNHLQ4IOAYnugwAXGRNmYxnb2GeKw=
github.com/acorn-io/etcd/server/v3 v3.5.1-ot-1 h1:MlyJCGYCmK9g7y3qjRQChBjDRLcjuHgxDG+BBM6+IVI=
Expand Down
2 changes: 1 addition & 1 deletion integration/helper/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
v1 "github.com/acorn-io/acorn/pkg/apis/internal.acorn.io/v1"
"github.com/acorn-io/acorn/pkg/client"
"github.com/acorn-io/acorn/pkg/controller"
"github.com/acorn-io/acorn/pkg/crds"
hclient "github.com/acorn-io/acorn/pkg/k8sclient"
"github.com/acorn-io/acorn/pkg/scheme"
"github.com/acorn-io/acorn/pkg/server"
"github.com/acorn-io/acorn/pkg/system"
"github.com/acorn-io/baaah/pkg/crds"
"github.com/acorn-io/baaah/pkg/restconfig"
"github.com/google/go-containerregistry/pkg/registry"
uuid2 "github.com/google/uuid"
Expand Down
33 changes: 15 additions & 18 deletions pkg/build/buildkit/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"context"
"fmt"

"github.com/acorn-io/acorn/pkg/k8sclient"
"github.com/acorn-io/acorn/pkg/system"
"github.com/acorn-io/baaah/pkg/restconfig"
"github.com/rancher/wrangler/pkg/apply"
"github.com/acorn-io/baaah/pkg/apply"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -41,47 +40,45 @@ func getRegistryPort(ctx context.Context, c client.Reader) (int, error) {
}

func deleteObjects(ctx context.Context) error {
cfg, err := restconfig.Default()
c, err := k8sclient.Default()
if err != nil {
return err
}
apply, err := apply.NewForConfig(cfg)

apply := apply.New(c)
if err != nil {
return err
}
return apply.
WithContext(ctx).
WithDynamicLookup().
WithSetID("acorn-buildkitd").
WithGVK(schema.GroupVersionKind{
WithOwnerSubContext("acorn-buildkitd").
WithPruneGVKs(schema.GroupVersionKind{
Version: "v1",
Kind: "Service",
}, schema.GroupVersionKind{
Group: "apps",
Version: "v1",
Kind: "Deployment",
}).
ApplyObjects()
Apply(ctx, nil)
}

func applyObjects(ctx context.Context) error {
cfg, err := restconfig.Default()
c, err := k8sclient.Default()
if err != nil {
return err
}
apply, err := apply.NewForConfig(cfg)

apply := apply.New(c)
if err != nil {
return err
}
return apply.
WithContext(ctx).
WithDynamicLookup().
WithSetID("acorn-buildkitd").
ApplyObjects(objects(system.Namespace, system.BuildkitImage, system.RegistryImage)...)
WithOwnerSubContext("acorn-buildkitd").
Apply(ctx, nil, objects(system.Namespace, system.BuildkitImage, system.RegistryImage)...)
}

func objects(namespace, buildKitImage, registryImage string) []runtime.Object {
return []runtime.Object{
func objects(namespace, buildKitImage, registryImage string) []client.Object {
return []client.Object{
&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: system.RegistryName,
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/acornrouter/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package acornrouter

import (
v1 "github.com/acorn-io/acorn/pkg/apis/internal.acorn.io/v1"
"github.com/acorn-io/baaah/pkg/apply"
"github.com/acorn-io/baaah/pkg/router"
"github.com/rancher/wrangler/pkg/apply"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down
11 changes: 4 additions & 7 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"context"

v1 "github.com/acorn-io/acorn/pkg/apis/internal.acorn.io/v1"
"github.com/acorn-io/acorn/pkg/crds"
"github.com/acorn-io/acorn/pkg/k8sclient"
"github.com/acorn-io/acorn/pkg/scheme"
"github.com/acorn-io/baaah"
"github.com/acorn-io/baaah/pkg/crds"
"github.com/acorn-io/baaah/pkg/apply"
"github.com/acorn-io/baaah/pkg/restconfig"
"github.com/acorn-io/baaah/pkg/router"
"github.com/rancher/wrangler/pkg/apply"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -38,18 +38,15 @@ func New() (*Controller, error) {
return nil, err
}

apply, err := apply.NewForConfig(cfg)
if err != nil {
return nil, err
}
apply := apply.New(client)

routes(router)

return &Controller{
Router: router,
client: client,
Scheme: scheme.Scheme,
apply: apply.WithDynamicLookup(),
apply: apply,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func (c *Controller) initData(ctx context.Context) error {
err := c.apply.WithSetID("acorn-controller-data").ApplyObjects(
err := c.apply.WithOwnerSubContext("acorn-controller-data").Apply(ctx, nil,
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: system.Namespace,
Expand Down
46 changes: 46 additions & 0 deletions pkg/crds/crds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package crds

import (
"context"

"github.com/acorn-io/baaah/pkg/restconfig"
"github.com/rancher/wrangler/pkg/crd"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
kclient "sigs.k8s.io/controller-runtime/pkg/client"
)

func Create(ctx context.Context, scheme *runtime.Scheme, gvs ...schema.GroupVersion) error {
var wranglerCRDs []crd.CRD

for _, gv := range gvs {
for kind := range scheme.KnownTypes(gv) {
gvk := gv.WithKind(kind)
obj, err := scheme.New(gvk)
if err != nil {
return err
}
_, isObj := obj.(kclient.Object)
_, isListObj := obj.(kclient.ObjectList)
if isObj && !isListObj {
wranglerCRDs = append(wranglerCRDs, crd.CRD{
GVK: gvk,
SchemaObject: obj,
Status: true,
}.WithColumnsFromStruct(obj))
}
}
}

restConfig, err := restconfig.New(scheme)
if err != nil {
return err
}

factory, err := crd.NewFactoryFromClient(restConfig)
if err != nil {
return err
}

return factory.BatchCreateCRDs(ctx, wranglerCRDs...).BatchWait()
}

0 comments on commit 1e8825c

Please sign in to comment.