Skip to content

Commit

Permalink
Merge gitjob into fleetcontroller
Browse files Browse the repository at this point in the history
  • Loading branch information
manno committed Apr 25, 2024
1 parent 0dfcd3b commit 973c995
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 85 deletions.
3 changes: 0 additions & 3 deletions .github/scripts/build-fleet-binaries.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,3 @@ go build -gcflags='all=-N -l' -o bin/fleetcontroller-linux-"$GOARCH" ./cmd/fleet
# fleet agent
go build -gcflags='all=-N -l' -o "bin/fleet-linux-$GOARCH" ./cmd/fleetcli
go build -gcflags='all=-N -l' -o "bin/fleetagent-linux-$GOARCH" ./cmd/fleetagent

# gitjob
go build -gcflags='all=-N -l' -o "bin/gitjob-linux-$GOARCH" ./cmd/gitjob
20 changes: 0 additions & 20 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ archives:
builds:
- fleet-agent

- id: fleet-gitjob
format: binary
name_template: "{{ .Binary }}"
builds:
- fleet-gitjob

- id: fleet-cli
format: binary
name_template: "{{ .Binary }}"
Expand Down Expand Up @@ -60,18 +54,6 @@ builds:
- linux_amd64_v1
- linux_arm64
- windows_amd64
-
id: fleet-gitjob
main: ./cmd/gitjob
binary: gitjob-{{ .Os }}-{{ .Arch }}{{ if .Arm }}64{{ end }}
no_unique_dist_dir: true
ldflags:
- -w -s
- -X github.com/rancher/fleet/pkg/version.GitCommit={{ .Commit }}
- -X github.com/rancher/fleet/pkg/version.Version={{ .Tag }}
targets:
- linux_amd64_v1
- linux_arm64
-
id: fleet-cli
main: ./cmd/fleetcli
Expand Down Expand Up @@ -112,7 +94,6 @@ dockers:
ids:
- fleet-controller
- fleet-cli
- fleet-gitjob

# Templates of the Docker image names.
image_templates:
Expand Down Expand Up @@ -140,7 +121,6 @@ dockers:
ids:
- fleet-controller
- fleet-cli
- fleet-gitjob
image_templates:
- "docker.io/rancher/fleet:{{ .Tag }}-linux-arm64"
dockerfile: package/Dockerfile
Expand Down
1 change: 1 addition & 0 deletions charts/fleet/templates/deployment_gitjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ spec:
- image: "{{ template "system_default_registry" . }}{{ .Values.image.repository }}:{{ .Values.image.tag }}"
name: gitjob
args:
- fleetcontroller
- gitjob
- --gitjob-image
- "{{ template "system_default_registry" . }}{{ .Values.image.repository }}:{{ .Values.image.tag }}"
Expand Down
93 changes: 38 additions & 55 deletions cmd/gitjob/main.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
//go:generate bash ./scripts/controller-gen-generate.sh

package main
package gitjob

import (
"context"
"flag"
"fmt"
"net/http"
"os"
"time"

command "github.com/rancher/fleet/internal/cmd"
controller "github.com/rancher/fleet/internal/cmd/gitjob"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"github.com/rancher/fleet/pkg/git/poll"
"github.com/rancher/fleet/pkg/version"
"github.com/rancher/fleet/pkg/webhook"
"github.com/spf13/cobra"

"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
clog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"

Expand All @@ -30,38 +32,53 @@ import (
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
zopts *zap.Options
)

func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(fleet.AddToScheme(scheme))
}

type flags struct {
metricsAddr string
enableLeaderElection bool
image string
listen string
debug bool
type GitOperator struct {
command.DebugConfig
Kubeconfig string `usage:"Kubeconfig file"`
Namespace string `usage:"namespace to watch" default:"cattle-fleet-system" env:"NAMESPACE"`
MetricsAddr string `name:"metrics-bind-address" default:":8081" usage:"The address the metric endpoint binds to."`
EnableLeaderElection bool `name:"leader-elect" default:"true" usage:"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager."`
Image string `name:"gitjob-image" default:"rancher/fleet:dev" usage:"The gitjob image that will be used in the generated job."`
Listen string `default:":8080" usage:"The port the webhook listens."`
}

func main() {
ctx := ctrl.SetupSignalHandler()
if err := run(ctx); err != nil {
setupLog.Error(err, "error running gitjob")
os.Exit(1)
func App(zo *zap.Options) *cobra.Command {
zopts = zo
return command.Command(&GitOperator{}, cobra.Command{
Version: version.FriendlyVersion(),
Use: "gitjob",
})
}

func (g *GitOperator) PersistentPre(_ *cobra.Command, _ []string) error {
if err := g.SetupDebug(); err != nil {
return fmt.Errorf("failed to setup debug logging: %w", err)
}

return nil
}

func run(ctx context.Context) error {
namespace := os.Getenv("NAMESPACE")
flags := bindFlags()
func (g *GitOperator) Run(cmd *cobra.Command, args []string) error {
ctx := clog.IntoContext(cmd.Context(), ctrl.Log)
// TODO for compatibility, override zap opts with legacy debug opts. remove once manifests are updated.
zopts.Development = g.Debug
ctrl.SetLogger(zap.New(zap.UseFlagOptions(zopts)))

namespace := g.Namespace
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: flags.metricsAddr,
BindAddress: g.MetricsAddr,
},
LeaderElection: flags.enableLeaderElection,
LeaderElection: g.EnableLeaderElection,
LeaderElectionID: "gitjob-leader",
LeaderElectionNamespace: namespace,
})
Expand All @@ -71,14 +88,14 @@ func run(ctx context.Context) error {
reconciler := &controller.GitJobReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Image: flags.image,
Image: g.Image,
GitPoller: poll.NewHandler(ctx, mgr.GetClient()),
Log: ctrl.Log.WithName("gitjob-reconciler"),
}

group := errgroup.Group{}
group.Go(func() error {
return startWebhook(ctx, namespace, flags.listen, mgr.GetClient(), mgr.GetCache())
return startWebhook(ctx, namespace, g.Listen, mgr.GetClient(), mgr.GetCache())
})
group.Go(func() error {
setupLog.Info("starting manager")
Expand All @@ -92,40 +109,6 @@ func run(ctx context.Context) error {
return group.Wait()
}

func bindFlags() flags {
var metricsAddr string
var enableLeaderElection bool
var image string
var listen string
var debug bool
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8081", "The address the metric endpoint binds to.")
flag.StringVar(
&image,
"gitjob-image",
"rancher/fleet:dev",
"The gitjob image that will be used in the generated job.",
)
flag.StringVar(&listen, "listen", ":8080", "The port the webhook listens.")
flag.BoolVar(&enableLeaderElection, "leader-elect", true,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.BoolVar(&debug, "debug", false, "debug mode.")
opts := zap.Options{
Development: debug,
}
opts.BindFlags(flag.CommandLine)
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
flag.Parse()

return flags{
metricsAddr: metricsAddr,
enableLeaderElection: enableLeaderElection,
image: image,
listen: listen,
debug: debug,
}
}

func startWebhook(ctx context.Context, namespace string, addr string, client client.Client, cacheClient cache.Cache) error {
setupLog.Info("Setting up webhook listener")
handler, err := webhook.HandleHooks(ctx, namespace, client, cacheClient)
Expand Down
2 changes: 0 additions & 2 deletions dev/build-fleet
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ if ! git diff --quiet HEAD origin/main -- pkg/apis/fleet.cattle.io/v1alpha1; th
fi

export GOOS=linux
# gitjob
go build -gcflags='all=-N -l' -o "bin/gitjob-linux-$GOARCH" ./cmd/gitjob

# fleet
go build -gcflags='all=-N -l' -o "bin/fleet-linux-$GOARCH" ./cmd/fleetcli
Expand Down
8 changes: 6 additions & 2 deletions internal/cmd/controller/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strconv"
"time"

"github.com/rancher/fleet/cmd/gitjob"
"github.com/rancher/fleet/internal/cmd/controller/agentmanagement"
"github.com/rancher/fleet/internal/cmd/controller/agentmanagement/agent"

Expand Down Expand Up @@ -156,8 +157,11 @@ func App() *cobra.Command {
ctrl.RegisterFlags(fs)
root.Flags().AddGoFlagSet(fs)

root.AddCommand(cleanup.App())
root.AddCommand(agentmanagement.App())
root.AddCommand(
cleanup.App(),
agentmanagement.App(),
gitjob.App(&zopts),
)
return root
}

Expand Down
3 changes: 0 additions & 3 deletions package/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,16 @@ FROM base AS copy_dapper
ONBUILD ARG ARCH
ONBUILD COPY bin/fleetcontroller-linux-$ARCH /usr/bin/fleetcontroller
ONBUILD COPY bin/fleet-linux-$ARCH /usr/bin/fleet
ONBUILD COPY bin/gitjob-linux-$ARCH /usr/bin/gitjob

FROM base AS copy_buildx
ONBUILD ARG TARGETARCH
ONBUILD COPY bin/fleetcontroller-linux-$TARGETARCH /usr/bin/fleetcontroller
ONBUILD COPY bin/fleet-linux-$TARGETARCH /usr/bin/fleet
ONBUILD COPY bin/gitjob-linux-$TARGETARCH /usr/bin/gitjob

FROM base AS copy_goreleaser
ONBUILD ARG ARCH
ONBUILD COPY fleetcontroller-linux-$ARCH /usr/bin/fleetcontroller
ONBUILD COPY fleet-linux-$ARCH /usr/bin/fleet
ONBUILD COPY gitjob-linux-$ARCH /usr/bin/gitjob

FROM copy_${BUILD_ENV}
USER 1000
Expand Down

0 comments on commit 973c995

Please sign in to comment.