github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/cmd/orbctl/api.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/spf13/cobra"
     7  
     8  	boomapi "github.com/caos/orbos/internal/operator/boom/api"
     9  	"github.com/caos/orbos/internal/operator/orbiter"
    10  	orbadapter "github.com/caos/orbos/internal/operator/orbiter/kinds/orb"
    11  	"github.com/caos/orbos/mntr"
    12  	"github.com/caos/orbos/pkg/git"
    13  	"github.com/caos/orbos/pkg/kubernetes/cli"
    14  	"github.com/caos/orbos/pkg/labels"
    15  )
    16  
    17  func APICommand(getRv GetRootValues) *cobra.Command {
    18  	var (
    19  		cmd = &cobra.Command{
    20  			Use:   "api",
    21  			Short: "Upgrade the yml-files to the newest version",
    22  			Long:  "Upgrade the yml-files to the newest version",
    23  		}
    24  	)
    25  
    26  	cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
    27  
    28  		rv := getRv("api", "", nil)
    29  		defer rv.ErrFunc(err)
    30  
    31  		if !rv.Gitops {
    32  			return mntr.ToUserError(errors.New("api command is only supported with the --gitops flag"))
    33  		}
    34  
    35  		if _, err := cli.Init(rv.Monitor, rv.OrbConfig, rv.GitClient, rv.Kubeconfig, rv.Gitops, true, true); err != nil {
    36  			return err
    37  		}
    38  
    39  		var desireds []git.GitDesiredState
    40  		if rv.GitClient.Exists(git.OrbiterFile) {
    41  			_, _, _, migrate, desired, _, _, err := orbiter.Adapt(rv.GitClient, monitor, make(chan struct{}), orbadapter.AdaptFunc(
    42  				labels.NoopOperator("ORBOS"),
    43  				rv.OrbConfig,
    44  				gitCommit,
    45  				true,
    46  				false,
    47  				rv.GitClient,
    48  			))
    49  			if err != nil {
    50  				return err
    51  			}
    52  
    53  			if migrate {
    54  				desireds = append(desireds, git.GitDesiredState{
    55  					Desired: desired,
    56  					Path:    git.OrbiterFile,
    57  				})
    58  			}
    59  		}
    60  		if rv.GitClient.Exists(git.BoomFile) {
    61  
    62  			desired, err := rv.GitClient.ReadTree(git.BoomFile)
    63  			if err != nil {
    64  				return err
    65  			}
    66  
    67  			toolset, migrate, _, _, err := boomapi.ParseToolset(desired)
    68  			if err != nil {
    69  				return err
    70  			}
    71  			if migrate {
    72  				desired.Parsed = toolset
    73  				desireds = append(desireds, git.GitDesiredState{
    74  					Desired: desired,
    75  					Path:    git.BoomFile,
    76  				})
    77  			}
    78  		}
    79  		if len(desireds) > 0 {
    80  			return rv.GitClient.PushGitDesiredStates(monitor, "migrate apis", desireds)
    81  		}
    82  		return nil
    83  	}
    84  	return cmd
    85  }