From 86335aa4ae01dadecd36468409d742e68b76925d Mon Sep 17 00:00:00 2001 From: baude Date: Mon, 14 Dec 2020 11:33:25 -0600 Subject: misc bindings to podman v3 manifest, system, info, volumes, play, and generate bindings are updated to always have binding options. Signed-off-by: baude --- pkg/bindings/generate/generate.go | 35 ++--- pkg/bindings/generate/types.go | 27 ++++ pkg/bindings/generate/types_kube_options.go | 104 +++++++++++++ pkg/bindings/generate/types_systemd_options.go | 200 +++++++++++++++++++++++++ 4 files changed, 347 insertions(+), 19 deletions(-) create mode 100644 pkg/bindings/generate/types.go create mode 100644 pkg/bindings/generate/types_kube_options.go create mode 100644 pkg/bindings/generate/types_systemd_options.go (limited to 'pkg/bindings/generate') diff --git a/pkg/bindings/generate/generate.go b/pkg/bindings/generate/generate.go index 8d0146ec1..29eb39557 100644 --- a/pkg/bindings/generate/generate.go +++ b/pkg/bindings/generate/generate.go @@ -4,31 +4,23 @@ import ( "context" "errors" "net/http" - "net/url" - "strconv" "github.com/containers/podman/v2/pkg/bindings" "github.com/containers/podman/v2/pkg/domain/entities" ) -func Systemd(ctx context.Context, nameOrID string, options entities.GenerateSystemdOptions) (*entities.GenerateSystemdReport, error) { +func Systemd(ctx context.Context, nameOrID string, options *SystemdOptions) (*entities.GenerateSystemdReport, error) { + if options == nil { + options = new(SystemdOptions) + } conn, err := bindings.GetClient(ctx) if err != nil { return nil, err } - params := url.Values{} - - params.Set("useName", strconv.FormatBool(options.Name)) - params.Set("new", strconv.FormatBool(options.New)) - if options.RestartPolicy != "" { - params.Set("restartPolicy", options.RestartPolicy) - } - if options.StopTimeout != nil { - params.Set("stopTimeout", strconv.FormatUint(uint64(*options.StopTimeout), 10)) + params, err := options.ToParams() + if err != nil { + return nil, err } - params.Set("containerPrefix", options.ContainerPrefix) - params.Set("podPrefix", options.PodPrefix) - params.Set("separator", options.Separator) response, err := conn.DoRequest(nil, http.MethodGet, "/generate/%s/systemd", params, nil, nameOrID) if err != nil { @@ -38,7 +30,10 @@ func Systemd(ctx context.Context, nameOrID string, options entities.GenerateSyst return report, response.Process(&report.Units) } -func Kube(ctx context.Context, nameOrIDs []string, options entities.GenerateKubeOptions) (*entities.GenerateKubeReport, error) { +func Kube(ctx context.Context, nameOrIDs []string, options *KubeOptions) (*entities.GenerateKubeReport, error) { + if options == nil { + options = new(KubeOptions) + } conn, err := bindings.GetClient(ctx) if err != nil { return nil, err @@ -46,12 +41,14 @@ func Kube(ctx context.Context, nameOrIDs []string, options entities.GenerateKube if len(nameOrIDs) < 1 { return nil, errors.New("must provide the name or ID of one container or pod") } - params := url.Values{} + + params, err := options.ToParams() + if err != nil { + return nil, err + } for _, name := range nameOrIDs { params.Add("names", name) } - params.Set("service", strconv.FormatBool(options.Service)) - response, err := conn.DoRequest(nil, http.MethodGet, "/generate/kube", params, nil) if err != nil { return nil, err diff --git a/pkg/bindings/generate/types.go b/pkg/bindings/generate/types.go new file mode 100644 index 000000000..2edf58768 --- /dev/null +++ b/pkg/bindings/generate/types.go @@ -0,0 +1,27 @@ +package generate + +//go:generate go run ../generator/generator.go KubeOptions +// KubeOptions are optional options for generating kube YAML files +type KubeOptions struct { + // Service - generate YAML for a Kubernetes _service_ object. + Service *bool +} + +//go:generate go run ../generator/generator.go SystemdOptions +// SystemdOptions are optional options for generating ssytemd files +type SystemdOptions struct { + // Name - use container/pod name instead of its ID. + UseName *bool + // New - create a new container instead of starting a new one. + New *bool + // RestartPolicy - systemd restart policy. + RestartPolicy *string + // StopTimeout - time when stopping the container. + StopTimeout *uint + // ContainerPrefix - systemd unit name prefix for containers + ContainerPrefix *string + // PodPrefix - systemd unit name prefix for pods + PodPrefix *string + // Separator - systemd unit name separator between name/id and prefix + Separator *string +} diff --git a/pkg/bindings/generate/types_kube_options.go b/pkg/bindings/generate/types_kube_options.go new file mode 100644 index 000000000..fbb26f554 --- /dev/null +++ b/pkg/bindings/generate/types_kube_options.go @@ -0,0 +1,104 @@ +package generate + +import ( + "net/url" + "reflect" + "strconv" + + jsoniter "github.com/json-iterator/go" + "github.com/pkg/errors" +) + +/* +This file is generated automatically by go generate. Do not edit. + +Created 2020-12-17 09:35:23.528143172 -0600 CST m=+0.000203394 +*/ + +// Changed +func (o *KubeOptions) Changed(fieldName string) bool { + r := reflect.ValueOf(o) + value := reflect.Indirect(r).FieldByName(fieldName) + return !value.IsNil() +} + +// ToParams +func (o *KubeOptions) ToParams() (url.Values, error) { + params := url.Values{} + if o == nil { + return params, nil + } + json := jsoniter.ConfigCompatibleWithStandardLibrary + s := reflect.ValueOf(o) + if reflect.Ptr == s.Kind() { + s = s.Elem() + } + sType := s.Type() + for i := 0; i < s.NumField(); i++ { + fieldName := sType.Field(i).Name + if !o.Changed(fieldName) { + continue + } + f := s.Field(i) + if reflect.Ptr == f.Kind() { + f = f.Elem() + } + switch f.Kind() { + case reflect.Bool: + params.Set(fieldName, strconv.FormatBool(f.Bool())) + case reflect.String: + params.Set(fieldName, f.String()) + case reflect.Int, reflect.Int64: + // f.Int() is always an int64 + params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) + case reflect.Uint, reflect.Uint64: + // f.Uint() is always an uint64 + params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) + case reflect.Slice: + typ := reflect.TypeOf(f.Interface()).Elem() + slice := reflect.MakeSlice(reflect.SliceOf(typ), f.Len(), f.Cap()) + switch typ.Kind() { + case reflect.String: + s, ok := slice.Interface().([]string) + if !ok { + return nil, errors.New("failed to convert to string slice") + } + for _, val := range s { + params.Add(fieldName, val) + } + default: + return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) + } + case reflect.Map: + lowerCaseKeys := make(map[string][]string) + iter := f.MapRange() + for iter.Next() { + lowerCaseKeys[iter.Key().Interface().(string)] = iter.Value().Interface().([]string) + + } + s, err := json.MarshalToString(lowerCaseKeys) + if err != nil { + return nil, err + } + + params.Set(fieldName, s) + } + } + return params, nil +} + +// WithService +func (o *KubeOptions) WithService(value bool) *KubeOptions { + v := &value + o.Service = v + return o +} + +// GetService +func (o *KubeOptions) GetService() bool { + var service bool + if o.Service == nil { + return service + } + return *o.Service +} diff --git a/pkg/bindings/generate/types_systemd_options.go b/pkg/bindings/generate/types_systemd_options.go new file mode 100644 index 000000000..20e032f5a --- /dev/null +++ b/pkg/bindings/generate/types_systemd_options.go @@ -0,0 +1,200 @@ +package generate + +import ( + "net/url" + "reflect" + "strconv" + + jsoniter "github.com/json-iterator/go" + "github.com/pkg/errors" +) + +/* +This file is generated automatically by go generate. Do not edit. + +Created 2020-12-17 09:35:23.663318384 -0600 CST m=+0.000158454 +*/ + +// Changed +func (o *SystemdOptions) Changed(fieldName string) bool { + r := reflect.ValueOf(o) + value := reflect.Indirect(r).FieldByName(fieldName) + return !value.IsNil() +} + +// ToParams +func (o *SystemdOptions) ToParams() (url.Values, error) { + params := url.Values{} + if o == nil { + return params, nil + } + json := jsoniter.ConfigCompatibleWithStandardLibrary + s := reflect.ValueOf(o) + if reflect.Ptr == s.Kind() { + s = s.Elem() + } + sType := s.Type() + for i := 0; i < s.NumField(); i++ { + fieldName := sType.Field(i).Name + if !o.Changed(fieldName) { + continue + } + f := s.Field(i) + if reflect.Ptr == f.Kind() { + f = f.Elem() + } + switch f.Kind() { + case reflect.Bool: + params.Set(fieldName, strconv.FormatBool(f.Bool())) + case reflect.String: + params.Set(fieldName, f.String()) + case reflect.Int, reflect.Int64: + // f.Int() is always an int64 + params.Set(fieldName, strconv.FormatInt(f.Int(), 10)) + case reflect.Uint, reflect.Uint64: + // f.Uint() is always an uint64 + params.Set(fieldName, strconv.FormatUint(f.Uint(), 10)) + case reflect.Slice: + typ := reflect.TypeOf(f.Interface()).Elem() + slice := reflect.MakeSlice(reflect.SliceOf(typ), f.Len(), f.Cap()) + switch typ.Kind() { + case reflect.String: + s, ok := slice.Interface().([]string) + if !ok { + return nil, errors.New("failed to convert to string slice") + } + for _, val := range s { + params.Add(fieldName, val) + } + default: + return nil, errors.Errorf("unknown slice type %s", f.Kind().String()) + } + case reflect.Map: + lowerCaseKeys := make(map[string][]string) + iter := f.MapRange() + for iter.Next() { + lowerCaseKeys[iter.Key().Interface().(string)] = iter.Value().Interface().([]string) + + } + s, err := json.MarshalToString(lowerCaseKeys) + if err != nil { + return nil, err + } + + params.Set(fieldName, s) + } + } + return params, nil +} + +// WithUseName +func (o *SystemdOptions) WithUseName(value bool) *SystemdOptions { + v := &value + o.UseName = v + return o +} + +// GetUseName +func (o *SystemdOptions) GetUseName() bool { + var useName bool + if o.UseName == nil { + return useName + } + return *o.UseName +} + +// WithNew +func (o *SystemdOptions) WithNew(value bool) *SystemdOptions { + v := &value + o.New = v + return o +} + +// GetNew +func (o *SystemdOptions) GetNew() bool { + var new bool + if o.New == nil { + return new + } + return *o.New +} + +// WithRestartPolicy +func (o *SystemdOptions) WithRestartPolicy(value string) *SystemdOptions { + v := &value + o.RestartPolicy = v + return o +} + +// GetRestartPolicy +func (o *SystemdOptions) GetRestartPolicy() string { + var restartPolicy string + if o.RestartPolicy == nil { + return restartPolicy + } + return *o.RestartPolicy +} + +// WithStopTimeout +func (o *SystemdOptions) WithStopTimeout(value uint) *SystemdOptions { + v := &value + o.StopTimeout = v + return o +} + +// GetStopTimeout +func (o *SystemdOptions) GetStopTimeout() uint { + var stopTimeout uint + if o.StopTimeout == nil { + return stopTimeout + } + return *o.StopTimeout +} + +// WithContainerPrefix +func (o *SystemdOptions) WithContainerPrefix(value string) *SystemdOptions { + v := &value + o.ContainerPrefix = v + return o +} + +// GetContainerPrefix +func (o *SystemdOptions) GetContainerPrefix() string { + var containerPrefix string + if o.ContainerPrefix == nil { + return containerPrefix + } + return *o.ContainerPrefix +} + +// WithPodPrefix +func (o *SystemdOptions) WithPodPrefix(value string) *SystemdOptions { + v := &value + o.PodPrefix = v + return o +} + +// GetPodPrefix +func (o *SystemdOptions) GetPodPrefix() string { + var podPrefix string + if o.PodPrefix == nil { + return podPrefix + } + return *o.PodPrefix +} + +// WithSeparator +func (o *SystemdOptions) WithSeparator(value string) *SystemdOptions { + v := &value + o.Separator = v + return o +} + +// GetSeparator +func (o *SystemdOptions) GetSeparator() string { + var separator string + if o.Separator == nil { + return separator + } + return *o.Separator +} -- cgit v1.2.3-54-g00ecf