|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/google/go-containerregistry/pkg/name" |
| 8 | + "knative.dev/func/pkg/builders" |
| 9 | + "knative.dev/func/pkg/buildpacks" |
| 10 | + "knative.dev/func/pkg/config" |
| 11 | + "knative.dev/func/pkg/creds" |
| 12 | + "knative.dev/func/pkg/docker" |
| 13 | + fn "knative.dev/func/pkg/functions" |
| 14 | + fnhttp "knative.dev/func/pkg/http" |
| 15 | + "knative.dev/func/pkg/k8s" |
| 16 | + "knative.dev/func/pkg/keda" |
| 17 | + "knative.dev/func/pkg/knative" |
| 18 | + "knative.dev/func/pkg/oci" |
| 19 | + "knative.dev/func/pkg/pipelines/tekton" |
| 20 | + "knative.dev/func/pkg/s2i" |
| 21 | +) |
| 22 | + |
| 23 | +// builderClientOptions returns client options for the given builder name. |
| 24 | +func builderClientOptions(builder string, cfg ClientConfig, withTimestamp bool, registryAuthfile string) ([]fn.Option, error) { |
| 25 | + if builder == "" { |
| 26 | + builder = builders.Pack |
| 27 | + } |
| 28 | + |
| 29 | + o := []fn.Option{ |
| 30 | + fn.WithVerbose(cfg.Verbose), |
| 31 | + } |
| 32 | + |
| 33 | + t := fnhttp.NewRoundTripper( |
| 34 | + fnhttp.WithInsecureSkipVerify(cfg.InsecureSkipVerify), |
| 35 | + fnhttp.WithOpenShiftServiceCA(), |
| 36 | + ) |
| 37 | + credsProvider := newCredentialsProvider(config.Dir(), t, registryAuthfile, cfg.InsecureSkipVerify) |
| 38 | + |
| 39 | + switch builder { |
| 40 | + case builders.Host: |
| 41 | + o = append(o, |
| 42 | + fn.WithScaffolder(oci.NewScaffolder(cfg.Verbose)), |
| 43 | + fn.WithBuilder(oci.NewBuilder(builders.Host, cfg.Verbose)), |
| 44 | + fn.WithPusher(oci.NewPusher(cfg.InsecureSkipVerify, false, cfg.Verbose, |
| 45 | + oci.WithTransport(fnhttp.NewRoundTripper(fnhttp.WithInsecureSkipVerify(cfg.InsecureSkipVerify), fnhttp.WithOpenShiftServiceCA())), |
| 46 | + oci.WithCredentialsProvider(credsProvider), |
| 47 | + oci.WithVerbose(cfg.Verbose))), |
| 48 | + ) |
| 49 | + case builders.Pack: |
| 50 | + o = append(o, |
| 51 | + fn.WithScaffolder(buildpacks.NewScaffolder(cfg.Verbose)), |
| 52 | + fn.WithBuilder(buildpacks.NewBuilder( |
| 53 | + buildpacks.WithName(builders.Pack), |
| 54 | + buildpacks.WithTimestamp(withTimestamp), |
| 55 | + buildpacks.WithVerbose(cfg.Verbose))), |
| 56 | + fn.WithPusher(docker.NewPusher( |
| 57 | + docker.WithCredentialsProvider(credsProvider), |
| 58 | + docker.WithTransport(t), |
| 59 | + docker.WithVerbose(cfg.Verbose), |
| 60 | + docker.WithInsecure(cfg.InsecureSkipVerify))), |
| 61 | + ) |
| 62 | + case builders.S2I: |
| 63 | + o = append(o, |
| 64 | + fn.WithScaffolder(s2i.NewScaffolder(cfg.Verbose)), |
| 65 | + fn.WithBuilder(s2i.NewBuilder( |
| 66 | + s2i.WithName(builders.S2I), |
| 67 | + s2i.WithVerbose(cfg.Verbose))), |
| 68 | + fn.WithPusher(docker.NewPusher( |
| 69 | + docker.WithCredentialsProvider(credsProvider), |
| 70 | + docker.WithTransport(t), |
| 71 | + docker.WithVerbose(cfg.Verbose), |
| 72 | + docker.WithInsecure(cfg.InsecureSkipVerify))), |
| 73 | + ) |
| 74 | + default: |
| 75 | + return o, builders.ErrUnknownBuilder{Name: builder, Known: builders.All()} |
| 76 | + } |
| 77 | + return o, nil |
| 78 | +} |
| 79 | + |
| 80 | +// deployClientOptions returns client options for a deploy operation. |
| 81 | +func deployClientOptions(builder, deployer string, cfg ClientConfig, withTimestamp bool) ([]fn.Option, error) { |
| 82 | + o, err := builderClientOptions(builder, cfg, withTimestamp, "") |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + |
| 87 | + t := fnhttp.NewRoundTripper( |
| 88 | + fnhttp.WithInsecureSkipVerify(cfg.InsecureSkipVerify), |
| 89 | + fnhttp.WithOpenShiftServiceCA(), |
| 90 | + ) |
| 91 | + credsProvider := newCredentialsProvider(config.Dir(), t, "", cfg.InsecureSkipVerify) |
| 92 | + |
| 93 | + o = append(o, fn.WithPipelinesProvider(tekton.NewPipelinesProvider( |
| 94 | + tekton.WithCredentialsProvider(credsProvider), |
| 95 | + tekton.WithVerbose(cfg.Verbose), |
| 96 | + tekton.WithTransport(t), |
| 97 | + ))) |
| 98 | + |
| 99 | + if deployer == "" { |
| 100 | + deployer = knative.KnativeDeployerName |
| 101 | + } |
| 102 | + switch deployer { |
| 103 | + case knative.KnativeDeployerName: |
| 104 | + o = append(o, fn.WithDeployer(knative.NewDeployer(knative.WithDeployerVerbose(cfg.Verbose)))) |
| 105 | + case k8s.KubernetesDeployerName: |
| 106 | + o = append(o, fn.WithDeployer(k8s.NewDeployer(k8s.WithDeployerVerbose(cfg.Verbose)))) |
| 107 | + case keda.KedaDeployerName: |
| 108 | + o = append(o, fn.WithDeployer(keda.NewDeployer(keda.WithDeployerVerbose(cfg.Verbose)))) |
| 109 | + default: |
| 110 | + return nil, fmt.Errorf("unsupported deployer: %s (supported: %s, %s, %s)", |
| 111 | + deployer, knative.KnativeDeployerName, k8s.KubernetesDeployerName, keda.KedaDeployerName) |
| 112 | + } |
| 113 | + return o, nil |
| 114 | +} |
| 115 | + |
| 116 | +func platformBuildOptions(platform string) ([]fn.BuildOption, error) { |
| 117 | + if platform == "" { |
| 118 | + return nil, nil |
| 119 | + } |
| 120 | + parts := strings.Split(platform, "/") |
| 121 | + if len(parts) != 2 { |
| 122 | + return nil, fmt.Errorf("platform must be in the form OS/Architecture (e.g. linux/amd64)") |
| 123 | + } |
| 124 | + return []fn.BuildOption{fn.BuildWithPlatforms([]fn.Platform{{OS: parts[0], Architecture: parts[1]}})}, nil |
| 125 | +} |
| 126 | + |
| 127 | +func shouldBuild(buildFlag string, f fn.Function) (bool, error) { |
| 128 | + if buildFlag == "" || buildFlag == "auto" { |
| 129 | + if f.Built() { |
| 130 | + return false, nil |
| 131 | + } |
| 132 | + return true, nil |
| 133 | + } |
| 134 | + build, err := parseBoolFlag(buildFlag) |
| 135 | + if err != nil { |
| 136 | + return false, fmt.Errorf("invalid build flag %q: must be 'auto' or a boolean", buildFlag) |
| 137 | + } |
| 138 | + return build, nil |
| 139 | +} |
| 140 | + |
| 141 | +func parseBoolFlag(v string) (bool, error) { |
| 142 | + switch strings.ToLower(v) { |
| 143 | + case "true", "1", "t", "yes": |
| 144 | + return true, nil |
| 145 | + case "false", "0", "f", "no": |
| 146 | + return false, nil |
| 147 | + default: |
| 148 | + return false, fmt.Errorf("invalid boolean value %q", v) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func isDigestedImage(v string) (bool, error) { |
| 153 | + ref, err := name.ParseReference(v) |
| 154 | + if err != nil { |
| 155 | + return false, err |
| 156 | + } |
| 157 | + _, ok := ref.(name.Digest) |
| 158 | + return ok, nil |
| 159 | +} |
| 160 | + |
| 161 | +func newCredentialsProvider(configPath string, t fnhttp.RoundTripCloser, authFilePath string, insecure bool) oci.CredentialsProvider { |
| 162 | + additionalLoaders := append(k8s.GetOpenShiftDockerCredentialLoaders(), k8s.GetGoogleCredentialLoader()...) |
| 163 | + additionalLoaders = append(additionalLoaders, k8s.GetECRCredentialLoader()...) |
| 164 | + additionalLoaders = append(additionalLoaders, k8s.GetACRCredentialLoader()...) |
| 165 | + |
| 166 | + options := []creds.Opt{ |
| 167 | + creds.WithTransport(t), |
| 168 | + creds.WithInsecure(insecure), |
| 169 | + creds.WithAdditionalCredentialLoaders(additionalLoaders...), |
| 170 | + } |
| 171 | + if authFilePath != "" { |
| 172 | + options = append(options, creds.WithAuthFilePath(authFilePath)) |
| 173 | + } |
| 174 | + return creds.NewCredentialsProvider(configPath, options...) |
| 175 | +} |
0 commit comments