2022-07-18 10:48:00 +00:00
//
// DISCLAIMER
//
2024-02-08 14:25:48 +00:00
// Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
2022-07-18 10:48:00 +00:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
package cmd
import (
"context"
2023-11-15 09:20:18 +00:00
"fmt"
2022-07-18 10:48:00 +00:00
"os"
2023-11-15 09:20:18 +00:00
"strconv"
"strings"
2022-07-18 10:48:00 +00:00
"time"
"github.com/spf13/cobra"
"github.com/arangodb/kube-arangodb/pkg/crd"
2023-11-15 09:20:18 +00:00
"github.com/arangodb/kube-arangodb/pkg/crd/crds"
2024-02-08 14:25:48 +00:00
"github.com/arangodb/kube-arangodb/pkg/util/errors"
2022-07-18 10:48:00 +00:00
"github.com/arangodb/kube-arangodb/pkg/util/kclient"
2024-04-29 09:56:10 +00:00
"github.com/arangodb/kube-arangodb/pkg/util/shutdown"
2022-07-18 10:48:00 +00:00
)
2024-06-17 10:53:18 +00:00
const (
AllSchemasValue = "all"
)
2022-07-18 10:48:00 +00:00
var (
cmdCRD = & cobra . Command {
Use : "crd" ,
Run : executeUsage ,
Short : "CRD operations" ,
}
cmdCRDInstall = & cobra . Command {
Use : "install" ,
Run : cmdCRDInstallRun ,
Short : "Install and update all required CRDs" ,
}
2024-06-17 10:53:18 +00:00
cmdCRDGenerate = & cobra . Command {
Use : "generate" ,
Run : cmdCRDGenerateRun ,
Short : "Generates YAML of all required CRDs" ,
}
2022-07-18 10:48:00 +00:00
)
2023-11-15 09:20:18 +00:00
var (
crdInstallOptions struct {
2024-06-17 10:53:18 +00:00
validationSchema [ ] string
preserveUnknownFields [ ] string
force bool
2023-11-15 09:20:18 +00:00
}
)
var (
defaultValidationSchemaEnabled [ ] string
)
2022-07-18 10:48:00 +00:00
func init ( ) {
cmdMain . AddCommand ( cmdCRD )
cmdOps . AddCommand ( cmdCRD )
2024-06-17 10:53:18 +00:00
f := cmdCRD . PersistentFlags ( )
2023-11-15 09:20:18 +00:00
f . StringArrayVar ( & crdInstallOptions . validationSchema , "crd.validation-schema" , defaultValidationSchemaEnabled , "Controls which CRD should have validation schema <crd-name>=<true/false>." )
2024-06-17 10:53:18 +00:00
f . StringArrayVar ( & crdInstallOptions . preserveUnknownFields , "crd.preserve-unknown-fields" , nil , "Controls which CRD should have enabled preserve unknown fields in validation schema <crd-name>=<true/false>." )
2024-03-22 10:37:31 +00:00
f . BoolVar ( & crdInstallOptions . force , "crd.force-update" , false , "Enforce CRD Schema update" )
2024-06-17 10:53:18 +00:00
2022-07-18 10:48:00 +00:00
cmdCRD . AddCommand ( cmdCRDInstall )
2024-06-17 10:53:18 +00:00
cmdCRD . AddCommand ( cmdCRDGenerate )
2022-07-18 10:48:00 +00:00
}
2024-06-17 10:53:18 +00:00
func prepareCRDOptions ( schemaEnabledArgs [ ] string , preserveUnknownFieldsArgs [ ] string ) ( map [ string ] crds . CRDOptions , error ) {
2023-11-15 09:20:18 +00:00
defaultOptions := crd . GetDefaultCRDOptions ( )
var err error
2024-06-17 10:53:18 +00:00
schemaEnabled := map [ string ] bool { }
preserveUnknownFields := map [ string ] bool { }
2023-11-15 09:20:18 +00:00
for _ , arg := range schemaEnabledArgs {
2024-06-17 10:53:18 +00:00
parts := strings . SplitN ( arg , "=" , 2 )
var enabled bool
if len ( parts ) == 2 {
enabled , err = strconv . ParseBool ( parts [ 1 ] )
if err != nil {
return nil , errors . Wrapf ( err , "not a bool value: %s" , parts [ 1 ] )
}
2023-11-15 09:20:18 +00:00
}
2024-06-17 10:53:18 +00:00
schemaEnabled [ parts [ 0 ] ] = enabled
}
for _ , arg := range preserveUnknownFieldsArgs {
parts := strings . SplitN ( arg , "=" , 2 )
var enabled bool
2023-11-15 09:20:18 +00:00
if len ( parts ) == 2 {
2024-06-17 10:53:18 +00:00
enabled , err = strconv . ParseBool ( parts [ 1 ] )
2023-11-15 09:20:18 +00:00
if err != nil {
return nil , errors . Wrapf ( err , "not a bool value: %s" , parts [ 1 ] )
}
2024-06-17 10:53:18 +00:00
}
preserveUnknownFields [ parts [ 0 ] ] = enabled
}
for k := range schemaEnabled {
if k == AllSchemasValue {
continue
}
if _ , ok := defaultOptions [ k ] ; ! ok {
return nil , fmt . Errorf ( "unknown CRD %s" , k )
2023-11-15 09:20:18 +00:00
}
2024-06-17 10:53:18 +00:00
}
for k := range preserveUnknownFields {
if k == AllSchemasValue {
continue
}
if _ , ok := defaultOptions [ k ] ; ! ok {
return nil , fmt . Errorf ( "unknown CRD %s" , k )
}
}
2023-11-15 09:20:18 +00:00
2024-06-17 10:53:18 +00:00
// Override the defaults
if v , ok := schemaEnabled [ AllSchemasValue ] ; ok {
delete ( preserveUnknownFields , AllSchemasValue )
for k := range defaultOptions {
z := defaultOptions [ k ]
z . WithSchema = v
defaultOptions [ k ] = z
}
}
if v , ok := preserveUnknownFields [ AllSchemasValue ] ; ok {
delete ( preserveUnknownFields , AllSchemasValue )
for k := range defaultOptions {
z := defaultOptions [ k ]
z . WithPreserve = v
defaultOptions [ k ] = z
}
}
// Set explicit words
for k , v := range schemaEnabled {
z := defaultOptions [ k ]
z . WithSchema = v
defaultOptions [ k ] = z
}
for k , v := range preserveUnknownFields {
z := defaultOptions [ k ]
z . WithPreserve = v
defaultOptions [ k ] = z
2023-11-15 09:20:18 +00:00
}
2024-06-17 10:53:18 +00:00
return defaultOptions , nil
2023-11-15 09:20:18 +00:00
}
2022-07-18 10:48:00 +00:00
func cmdCRDInstallRun ( cmd * cobra . Command , args [ ] string ) {
2024-06-17 10:53:18 +00:00
crdOpts , err := prepareCRDOptions ( crdInstallOptions . validationSchema , crdInstallOptions . preserveUnknownFields )
2023-11-15 09:20:18 +00:00
if err != nil {
logger . Fatal ( "Invalid --crd.validation-schema args: %s" , err )
return
}
2022-07-18 10:48:00 +00:00
client , ok := kclient . GetDefaultFactory ( ) . Client ( )
if ! ok {
logger . Fatal ( "Failed to get client" )
2023-11-15 09:20:18 +00:00
return
2022-07-18 10:48:00 +00:00
}
2024-04-29 09:56:10 +00:00
ctx , cancel := context . WithTimeout ( shutdown . Context ( ) , time . Minute )
2022-07-18 10:48:00 +00:00
defer cancel ( )
2024-03-22 10:37:31 +00:00
err = crd . EnsureCRDWithOptions ( ctx , client , crd . EnsureCRDOptions { IgnoreErrors : false , CRDOptions : crdOpts , ForceUpdate : crdInstallOptions . force } )
2022-07-18 10:48:00 +00:00
if err != nil {
os . Exit ( 1 )
}
}
2024-06-17 10:53:18 +00:00
func cmdCRDGenerateRun ( cmd * cobra . Command , args [ ] string ) {
crdOpts , err := prepareCRDOptions ( crdInstallOptions . validationSchema , crdInstallOptions . preserveUnknownFields )
if err != nil {
logger . Fatal ( "Invalid --crd.validation-schema args: %s" , err )
return
}
err = crd . GenerateCRDYAMLWithOptions ( crd . EnsureCRDOptions { IgnoreErrors : false , CRDOptions : crdOpts , ForceUpdate : crdInstallOptions . force } , cmd . OutOrStdout ( ) )
if err != nil {
os . Exit ( 1 )
}
}