...

Source file src/edge-infra.dev/cmd/edge/datasync/datasyncinit/main.go

Documentation: edge-infra.dev/cmd/edge/datasync/datasyncinit

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"flag"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/go-logr/logr"
    11  	"github.com/peterbourgon/ff/v3"
    12  
    13  	dsapi "edge-infra.dev/pkg/edge/datasync/apis/v1alpha1"
    14  	"edge-infra.dev/pkg/edge/datasync/datasyncinit"
    15  	"edge-infra.dev/pkg/lib/logging"
    16  
    17  	"k8s.io/apimachinery/pkg/runtime"
    18  	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
    19  	clientgoscheme "k8s.io/client-go/kubernetes/scheme"
    20  	"k8s.io/client-go/rest"
    21  	"k8s.io/client-go/tools/clientcmd"
    22  	"k8s.io/client-go/util/homedir"
    23  
    24  	"sigs.k8s.io/controller-runtime/pkg/client"
    25  )
    26  
    27  func main() {
    28  	ctx := context.Background()
    29  	log := logging.NewLogger().WithName("datasyncinit")
    30  
    31  	flags := flag.NewFlagSet("datasyncinit", flag.ExitOnError)
    32  	cfg := &datasyncinit.Config{}
    33  	cfg.BindFlags(flags)
    34  	if err := ff.Parse(flags, os.Args[1:], ff.WithEnvVarNoPrefix(), ff.WithIgnoreUndefined(true)); err != nil {
    35  		log.Error(err, "error parsing cmd arguments or env variables")
    36  	}
    37  	if err := cfg.Validate(); err != nil {
    38  		log.Error(err, "invalid config for workload initialization")
    39  		os.Exit(1)
    40  	}
    41  	log = log.WithValues("config", cfg)
    42  
    43  	log.Info("Workload initialization started")
    44  
    45  	config, err := rest.InClusterConfig()
    46  	if errors.Is(err, rest.ErrNotInCluster) {
    47  		config, err = clientcmd.BuildConfigFromFlags("", filepath.Join(homedir.HomeDir(), ".kube", "config"))
    48  		if err != nil {
    49  			log.Error(err, "fail to get local config")
    50  			os.Exit(1)
    51  		}
    52  	} else if err != nil {
    53  		log.Error(err, "fail to get in-cluster config")
    54  		os.Exit(1)
    55  	}
    56  
    57  	cl, err := client.NewWithWatch(config, client.Options{Scheme: createScheme()})
    58  	if err != nil {
    59  		log.Error(err, "fail to create new k8 client")
    60  		os.Exit(1)
    61  	}
    62  	ctx = logr.NewContext(ctx, log)
    63  	if err = datasyncinit.InitializeWorkload(ctx, cl, cfg); err != nil {
    64  		os.Exit(1)
    65  	}
    66  }
    67  
    68  func createScheme() *runtime.Scheme {
    69  	scheme := runtime.NewScheme()
    70  	utilruntime.Must(clientgoscheme.AddToScheme(scheme))
    71  	utilruntime.Must(dsapi.AddToScheme(scheme))
    72  	return scheme
    73  }
    74  

View as plain text