Skip to content

Commit

Permalink
Merge branch 'nephio-project:main' into pv-pipeline-readiness-gates
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesMcDermott authored Dec 12, 2024
2 parents ec61e77 + c017ed2 commit 636c719
Show file tree
Hide file tree
Showing 23 changed files with 328 additions and 113 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2024 The Nephio Authors
#
# 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.

name: golangci-lint
on:
push:
paths-ignore:
- "docs/**"
- "release/**"
- ".prow.yaml"
- "OWNERS"
pull_request:
paths-ignore:
- "docs/**"
- "release/**"
- ".prow.yaml"
- "OWNERS"
workflow_dispatch:

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read

env:
GO_VERSION: 1.22.0

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- name: Checkout Porch
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.62.2
args: --timeout=10m --go=${{ env.GO_VERSION }} --exclude-dirs=test --exclude-generated=true --issues-exit-code=0
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ __debug*

### VisualStudioCode Patch ###
# Ignore all local history of files
**/.history
**/.history

### Jetbrains IDEs ###
.idea/*
21 changes: 21 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",

// A configuration for running the porch server through vscode for the use of debugging.
// Assumes a cluster is set up alongside Git (./scripts/setup-dev-env.sh) and porch components (make ../porch/run-in-kind-no-server) are pre-configured
// With the configuration above one can use Vscode (Run & Debug) and launch server.
// This launches the porch server through vscode outside the cluster and the logs can be viewed in the debug console.
// Breakpoints can be added throughout the porch server code to debug.
"configurations": [
{
"name": "Launch Server",
Expand Down Expand Up @@ -36,6 +42,21 @@
"ENABLE_PACKAGEVARIANTSETS": "true"
}
},
// A configuration for running a porchctl command using the vscode debugger.
// Assumes a cluster is set up alongside Git (scripts/setup-dev-env.sh) and porch components (make run-in-kind) in the /porch directory are pre-configured
// This allows for the running of porchctl commands through vscode outside the cluster and the logs can be viewed in the debug console.
// Breakpoints can be added throughout the porch server code to debug.
{
"name": "Run Porchctl command",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/porchctl/main.go",
"args": [
"rpkg", "init", "porch-package-name", "--workspace=v1", "--namespace=repository-namespace", "--repository=repo-name"
],
"cwd": "${workspaceFolder}"
},
{
"name": "Launch test function",
"type": "go",
Expand Down
9 changes: 5 additions & 4 deletions default-go-lint.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The Nephio Authors.
# Copyright 2023-2024 The Nephio Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,7 +13,8 @@
# limitations under the License.


GOLANG_CI_VER ?= v1.57
GOLANG_CI_VER ?= v1.62.2
GO_VERSION ?= 1.22.0
GIT_ROOT_DIR ?= $(dir $(lastword $(MAKEFILE_LIST)))
include $(GIT_ROOT_DIR)/detect-container-runtime.mk

Expand All @@ -22,7 +23,7 @@ include $(GIT_ROOT_DIR)/detect-container-runtime.mk
lint: ## Run Go linter against the codebase
ifeq ($(CONTAINER_RUNNABLE), 0)
$(RUN_CONTAINER_COMMAND) docker.io/golangci/golangci-lint:${GOLANG_CI_VER}-alpine \
golangci-lint run ./... -v --timeout 10m
golangci-lint run ./... -v --go=${GO_VERSION} --timeout=10m --exclude-dirs=test --exclude-generated=true
else
golangci-lint run ./... -v --timeout 10m
golangci-lint run ./... -v --go=${GO_VERSION} --timeout=10m --exclude-dirs=test --exclude-generated=true
endif
21 changes: 15 additions & 6 deletions internal/kpt/fnruntime/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,22 @@ func NewContainerEnvFromStringSlice(envStr []string) *runtimeutil.ContainerEnv {
return ce
}

// ResolveToImageForCLI converts the function short path to the full image url.
// If the function is Catalog function, it adds "gcr.io/kpt-fn/".e.g. set-namespace:v0.1 --> gcr.io/kpt-fn/set-namespace:v0.1
func ResolveToImageForCLI(_ context.Context, image string) (string, error) {
if !strings.Contains(image, "/") {
return fmt.Sprintf("gcr.io/kpt-fn/%s", image), nil
// ResolveToImageForCLIFunc returns a func that converts the KRM function short path to the full image url.
// If the function is a catalog function, it prepends `prefix`, e.g. "set-namespace:v0.1" --> prefix + "set-namespace:v0.1".
// A "/" is appended to `prefix` if it is not an empty string and does not end with a "/".
func ResolveToImageForCLIFunc(prefix string) func(_ context.Context, image string) (string, error) {
prefix = strings.TrimSuffix(prefix, "/")
if prefix == "" {
return func(_ context.Context, image string) (string, error) {
return image, nil
}
}
return func(_ context.Context, image string) (string, error) {
if !strings.Contains(image, "/") {
return fmt.Sprintf("%s/%s", prefix, image), nil
}
return image, nil
}
return image, nil
}

// ContainerImageError is an error type which will be returned when
Expand Down
5 changes: 3 additions & 2 deletions internal/kpt/fnruntime/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (

const (
FuncGenPkgContext = "builtins/gen-pkg-context"
GCRImagePrefix = "gcr.io/kpt-fn/"
)

type RunnerOptions struct {
Expand Down Expand Up @@ -79,9 +80,9 @@ type RunnerOptions struct {
// ImageResolveFunc is the type for a function that can resolve a partial image to a (more) fully-qualified name
type ImageResolveFunc func(ctx context.Context, image string) (string, error)

func (o *RunnerOptions) InitDefaults() {
func (o *RunnerOptions) InitDefaults(defaultImagePrefix string) {
o.ImagePullPolicy = IfNotPresentPull
o.ResolveToImage = ResolveToImageForCLI
o.ResolveToImage = ResolveToImageForCLIFunc(defaultImagePrefix)
}

// NewRunner returns a FunctionRunner given a specification of a function
Expand Down
31 changes: 31 additions & 0 deletions internal/kpt/fnruntime/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,34 @@ func TestPrintFnStderr(t *testing.T) {
})
}
}

func TestRunnerOptions_InitDefaults(t *testing.T) {
tests := map[string]struct {
prefix string
}{
"empty": {prefix: ""},
"trailing_slash": {prefix: "example.org/kpt-fn/"},
"no_trailing_slash": {prefix: "example.org/kpt-fn"},
}

const fnName = "my-krm-function"

for testName, tc := range tests {
t.Run(testName, func(t *testing.T) {
opts := &RunnerOptions{}
opts.InitDefaults(tc.prefix)

result, err := opts.ResolveToImage(context.TODO(), fnName)

assert.NoError(t, err)
assert.Equal(t, getExpectedPrefix(tc.prefix)+fnName, result)
})
}
}

func getExpectedPrefix(prefix string) string {
if prefix != "" && !strings.HasSuffix(prefix, "/") {
return prefix + "/"
}
return prefix
}
10 changes: 9 additions & 1 deletion internal/kpt/util/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type Command struct {
// Kptfile. This determines how changes will be merged when updating the
// package.
UpdateStrategy kptfilev1.UpdateStrategyType

// DefaultKrmFunctionImagePrefix is the prefix to be used with unqualified
// KRM function image names. Defaults to "gcr.io/kpt-fn/".
DefaultKrmFunctionImagePrefix string
}

// Run runs the Command.
Expand Down Expand Up @@ -127,7 +131,7 @@ func (c Command) Run(ctx context.Context) error {
pr := printer.FromContextOrDie(ctx)
pr.Printf("\nCustomizing package for deployment.\n")
hookCmd := hook.Executor{}
hookCmd.RunnerOptions.InitDefaults()
hookCmd.RunnerOptions.InitDefaults(c.DefaultKrmFunctionImagePrefix)
hookCmd.PkgPath = c.Destination

builtinHooks := []kptfilev1.Function{
Expand Down Expand Up @@ -221,6 +225,10 @@ func (c *Command) DefaultValues() error {
c.UpdateStrategy = kptfilev1.ResourceMerge
}

if len(c.DefaultKrmFunctionImagePrefix) == 0 {
c.DefaultKrmFunctionImagePrefix = fnruntime.GCRImagePrefix
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (c completedConfig) New() (*PorchServer, error) {

runnerOptionsResolver := func(namespace string) fnruntime.RunnerOptions {
runnerOptions := fnruntime.RunnerOptions{}
runnerOptions.InitDefaults()
runnerOptions.InitDefaults(c.ExtraConfig.DefaultImagePrefix)

return runnerOptions
}
Expand Down
Loading

0 comments on commit 636c719

Please sign in to comment.