Skip to content

Commit

Permalink
A few more imports
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbelamaric committed Dec 18, 2023
1 parent 9b569cf commit 76bb838
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 12 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ require (
github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/set-namespace v0.4.1
github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/starlark v0.4.3
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20220506190241-f85503febd54
github.com/GoogleContainerTools/kpt/porch/api v0.0.0-20230121152246-dc44dbd18a33
github.com/bluekeyes/go-gitdiff v0.6.1
github.com/bytecodealliance/wasmtime-go v0.39.0
github.com/cpuguy83/go-md2man/v2 v2.0.2
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/starlark v0.4
github.com/GoogleContainerTools/kpt-functions-catalog/functions/go/starlark v0.4.3/go.mod h1:V11NRqrqOFy43O35azz012xtNBU/px2b8qclZn3Ohpg=
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20220506190241-f85503febd54 h1:z5iYiugZJiTzQ6ggU0Cae/T+LkrDwcqZyebh8SbmQ0E=
github.com/GoogleContainerTools/kpt-functions-sdk/go/fn v0.0.0-20220506190241-f85503febd54/go.mod h1:vl3iiwgrqdDgvGi5ckt3O9IoyaHUgFkfxE4RjQIqgwk=
github.com/GoogleContainerTools/kpt/porch/api v0.0.0-20230121152246-dc44dbd18a33 h1:9M1bvq7hU/JTY4VVcqhCQT0eAa5HznrFaLAm2ldfe70=
github.com/GoogleContainerTools/kpt/porch/api v0.0.0-20230121152246-dc44dbd18a33/go.mod h1:ASrhnLAL4ahTuiUJyepqcpVRXIoRMJyDs8/eSxwhgZM=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/Microsoft/go-winio v0.5.0/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
Expand Down
2 changes: 1 addition & 1 deletion internal/kpt/cmdutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"fmt"

"github.com/GoogleContainerTools/kpt/pkg/live"
"github.com/GoogleContainerTools/kpt/pkg/printer"
"github.com/nephio-project/porch/pkg/kpt/printer"
"k8s.io/kubectl/pkg/cmd/util"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/kpt/util/cmdutil/cmdutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"os"
"strings"

"github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
"github.com/nephio-project/porch/api/porch/v1alpha1"
"github.com/nephio-project/porch/internal/kpt/util/function"
"github.com/nephio-project/porch/internal/kpt/util/httputil"
"github.com/nephio-project/porch/internal/kpt/util/porch"
Expand Down
2 changes: 1 addition & 1 deletion internal/kpt/util/function/catalogfn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package function
import (
"fmt"

"github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
"github.com/nephio-project/porch/api/porch/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/kpt/util/function/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package function
import (
"fmt"

"github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
"github.com/nephio-project/porch/api/porch/v1alpha1"
)

type Matcher interface {
Expand Down
2 changes: 1 addition & 1 deletion internal/kpt/util/porch/approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"fmt"

"github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
"github.com/nephio-project/porch/api/porch/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
Expand Down
4 changes: 2 additions & 2 deletions internal/kpt/util/porch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package porch
import (
"context"

porchapi "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
configapi "github.com/GoogleContainerTools/kpt/porch/api/porchconfig/v1alpha1"
porchapi "github.com/nephio-project/porch/api/porch/v1alpha1"
configapi "github.com/nephio-project/porch/api/porchconfig/v1alpha1"
coreapi "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down
2 changes: 1 addition & 1 deletion internal/kpt/util/porch/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"time"

"github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
"github.com/nephio-project/porch/api/porch/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"context"
"fmt"

"github.com/GoogleContainerTools/kpt/pkg/kptpkg"
api "github.com/nephio-project/porch/api/porch/v1alpha1"
"github.com/nephio-project/porch/pkg/kpt/kptpkg"
"github.com/nephio-project/porch/pkg/kpt/printer"
"github.com/nephio-project/porch/pkg/kpt/printer/fake"
"github.com/nephio-project/porch/pkg/repository"
Expand Down
180 changes: 180 additions & 0 deletions pkg/kpt/kptpkg/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// Copyright 2022 The kpt 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.

package kptpkg

import (
"bytes"
"context"
"html/template"
"path/filepath"
"strings"

"github.com/nephio-project/porch/internal/kpt/builtins"
"github.com/nephio-project/porch/internal/kpt/pkg"
"github.com/nephio-project/porch/internal/kpt/util/man"
kptfilev1 "github.com/nephio-project/porch/pkg/kpt/api/kptfile/v1"
"github.com/nephio-project/porch/pkg/kpt/printer"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/kustomize/kyaml/kio/filters"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

// Initializer defines capability to initialize a kpt package.
type Initializer interface {
Initialize(ctx context.Context, pkg filesys.FileSystem, opts InitOptions) error
}

// InitOptions contains customization options for package initialization.
type InitOptions struct {
PkgName string
PkgPath string
// RelPath is used purely for printing info relative to current working dir of user.
// It may or may not be same as PkgPath.
RelPath string
Desc string
Keywords []string
Site string
}

// DefaultInitilizer implements Initializer interface.
type DefaultInitializer struct{}

func (i *DefaultInitializer) Initialize(
ctx context.Context,
fsys filesys.FileSystem,
opts InitOptions,
) error {
p, err := pkg.New(fsys, opts.PkgPath)
if err != nil {
return err
}

var pkgName string
if opts.PkgName != "" {
pkgName = opts.PkgName
} else {
pkgName = string(p.DisplayPath)
}

up := string(p.UniquePath)
if !fsys.Exists(string(p.UniquePath)) {
return errors.Errorf("%s does not exist", p.UniquePath)
}

pr := printer.FromContextOrDie(ctx)

if !fsys.Exists(filepath.Join(up, kptfilev1.KptFileName)) {
pr.Printf("writing %s\n", filepath.Join(opts.RelPath, "Kptfile"))
k := kptfilev1.KptFile{
ResourceMeta: yaml.ResourceMeta{
ObjectMeta: yaml.ObjectMeta{
NameMeta: yaml.NameMeta{
Name: pkgName,
},
// mark Kptfile as local-config
Annotations: map[string]string{
filters.LocalConfigAnnotation: "true",
},
},
},
Info: &kptfilev1.PackageInfo{
Description: opts.Desc,
Site: opts.Site,
Keywords: opts.Keywords,
},
}

// serialize the gvk when writing the Kptfile
k.Kind = kptfilev1.TypeMeta.Kind
k.APIVersion = kptfilev1.TypeMeta.APIVersion

err = func() error {
f, err := fsys.Create(filepath.Join(up, kptfilev1.KptFileName))
if err != nil {
return err
}
defer f.Close()
e := yaml.NewEncoder(f)

defer e.Close()
return e.Encode(k)
}()
if err != nil {
return err
}
}

if !fsys.Exists(filepath.Join(up, man.ManFilename)) {
pr.Printf("writing %s\n", filepath.Join(opts.RelPath, man.ManFilename))
buff := &bytes.Buffer{}
t, err := template.New("man").Parse(manTemplate)
if err != nil {
return err
}
templateData := map[string]string{
"Name": pkgName,
"Description": opts.Desc,
}

err = t.Execute(buff, templateData)
if err != nil {
return err
}

// Replace single quotes with backticks.
content := strings.ReplaceAll(buff.String(), "'", "`")

err = fsys.WriteFile(filepath.Join(up, man.ManFilename), []byte(content))
if err != nil {
return err
}
}

pkgContextPath := filepath.Join(up, builtins.PkgContextFile)
if !fsys.Exists(pkgContextPath) {
pr.Printf("writing %s\n", filepath.Join(opts.RelPath, builtins.PkgContextFile))
if err := fsys.WriteFile(pkgContextPath, []byte(builtins.AbstractPkgContext())); err != nil {
return err
}
}
return nil
}

// manTemplate is the content for the automatically generated README.md file.
// It uses ' instead of ` since golang doesn't allow using ` in a raw string
// literal. We do a replace on the content before printing.
var manTemplate = `# {{.Name}}
## Description
{{.Description}}
## Usage
### Fetch the package
'kpt pkg get REPO_URI[.git]/PKG_PATH[@VERSION] {{.Name}}'
Details: https://kpt.dev/reference/cli/pkg/get/
### View package content
'kpt pkg tree {{.Name}}'
Details: https://kpt.dev/reference/cli/pkg/tree/
### Apply the package
'''
kpt live init {{.Name}}
kpt live apply {{.Name}} --reconcile-timeout=2m --output=table
'''
Details: https://kpt.dev/reference/cli/live/
`

0 comments on commit 76bb838

Please sign in to comment.