Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record the progress of the OSPP 2023 hgctl project #453

Merged
merged 30 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
22e8d5c
feat: add command 'hgctl plugin build'
WeixinX Jul 27, 2023
ae10360
fix: fix something
WeixinX Jul 27, 2023
4f3d18b
fix: hgctl plugin build: modify some error handle and cmd example format
WeixinX Jul 31, 2023
421243b
feat: add command 'hgctl plugin install'
WeixinX Jul 31, 2023
d3ececd
fix: hgctl plugin install: add license header
WeixinX Jul 31, 2023
4f24309
feat: add command 'hgctl plugin uninstall'
WeixinX Jul 31, 2023
5b6348d
feat: add command 'hgctl plugin ls'
WeixinX Jul 31, 2023
6cae586
refactor: 'hgctl plugin build': create a dir named types
WeixinX Aug 8, 2023
fb99cc8
feat: add command 'hgctl plugin test'
WeixinX Aug 8, 2023
6fc9a4c
fix: hgctl plugin test: add license header
WeixinX Aug 11, 2023
459a5a0
feat: add command 'hgctl plugin config'
WeixinX Aug 11, 2023
84cfbb9
fix: annotation example
WeixinX Aug 22, 2023
5c33953
feat: add command 'hgctl plugin init'
WeixinX Aug 22, 2023
fd570bf
feat: add option
WeixinX Sep 4, 2023
82eaa96
refactor: 'hgctl plugin build'
WeixinX Sep 4, 2023
488d6b2
fix: 'hgctl plugin build': fix 'config' and something else
WeixinX Sep 4, 2023
17d3d5e
refactor: 'hgctl plugin test'
WeixinX Sep 4, 2023
13422b4
refactor: 'hgctl plugin install/uninstall'
WeixinX Sep 5, 2023
92f271a
refactor: pre test version
WeixinX Sep 7, 2023
e5a5dae
fix: Fixed an issue that was not deserialized due to orderedmap
WeixinX Sep 7, 2023
32a5980
fix: 'hgctl plugin buil': change tinygo cmd
WeixinX Sep 9, 2023
6c97dce
fix: docker compose sdk version degraded
WeixinX Sep 26, 2023
9db726b
fix: Merge lastest branch
WeixinX Sep 26, 2023
3cca24e
Merge branch 'main' of github.com:alibaba/higress into feat/ospp-hgctl
WeixinX Sep 26, 2023
5235ae1
fix: Add the license header
WeixinX Sep 26, 2023
ef1b81a
refactor: do some refectors for schema and meta
WeixinX Oct 6, 2023
40f2763
Merge branch 'main' of github.com:alibaba/higress into feat/ospp-hgctl
WeixinX Oct 6, 2023
2c8bb28
fix: Fix something
WeixinX Oct 19, 2023
8a84cc3
Merge branch 'main' of github.com:alibaba/higress into feat/ospp-hgctl
WeixinX Oct 27, 2023
8d8cd2c
fix: Change variable visibility and naming
WeixinX Oct 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 114 additions & 49 deletions go.mod

Large diffs are not rendered by default.

342 changes: 299 additions & 43 deletions go.sum

Large diffs are not rendered by default.

111 changes: 111 additions & 0 deletions pkg/cmd/hgctl/docker/compose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright (c) 2022 Alibaba Group Holding Ltd.
//
// 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 docker

import (
"context"
"io"
"strings"

"github.com/compose-spec/compose-go/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/flags"
"github.com/docker/compose/v2/cmd/formatter"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/compose"
)

type Compose struct {
client *api.ServiceProxy
w io.Writer
}

func NewCompose(w io.Writer) (*Compose, error) {
c := &Compose{w: w}

dockerCli, err := command.NewDockerCli(
command.WithCombinedStreams(c.w),
// command.WithDefaultContextStoreConfig(), Deprecated, set during NewDockerCli
)
if err != nil {
return nil, err
}

opts := flags.NewClientOptions()
err = dockerCli.Initialize(opts)
if err != nil {
return nil, err
}
c.client = api.NewServiceProxy().WithService(compose.NewComposeService(dockerCli.Client(), dockerCli.ConfigFile()))

return c, nil
}

func (c Compose) Up(ctx context.Context, name string, configs []string, source string, detach bool) error {
pOpts, err := cli.NewProjectOptions(
configs,
cli.WithWorkingDirectory(source),
cli.WithDefaultConfigPath,
cli.WithName(name),
)
if err != nil {
return err
}

project, err := cli.ProjectFromOptions(pOpts)
if err != nil {
return err
}

for i, s := range project.Services {
// TODO(WeixinX): Change from `Label` to `CustomLabels` after upgrading the dependency library github.com/compose-spec/compose-go
s.Labels = map[string]string{
api.ProjectLabel: project.Name,
api.ServiceLabel: s.Name,
api.VersionLabel: api.ComposeVersion,
api.WorkingDirLabel: project.WorkingDir,
api.ConfigFilesLabel: strings.Join(project.ComposeFiles, ","),
api.OneoffLabel: "False",
}
project.Services[i] = s
}
project.WithoutUnnecessaryResources()

// for log
var consumer api.LogConsumer
if !detach {
// TODO(WeixinX): Change to `formatter.NewLogConsumer(ctx, c.w, c.w, true, true, false)` after upgrading the dependency library github.com/compose-spec/compose-go
consumer = formatter.NewLogConsumer(ctx, c.w, true, true)
}
attachTo := make([]string, 0)
for _, svc := range project.Services {
attachTo = append(attachTo, svc.Name)
}

return c.client.Up(ctx, project, api.UpOptions{
Start: api.StartOptions{
Attach: consumer,
AttachTo: attachTo,
},
})
}

func (c Compose) List(ctx context.Context) ([]api.Stack, error) {
return c.client.List(ctx, api.ListOptions{})
}

func (c Compose) Down(ctx context.Context, name string) error {
return c.client.Down(ctx, name, api.DownOptions{})
}
130 changes: 130 additions & 0 deletions pkg/cmd/hgctl/kubernetes/wasmplugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright (c) 2022 Alibaba Group Holding Ltd.
//
// 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 kubernetes

import (
"context"

"github.com/spf13/pflag"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

const (
DefaultHigressNamespace = "higress-system"
HigressExtGroup = "extensions.higress.io"
HigressExtVersion = "v1alpha1"
HigressExtAPIVersion = HigressExtGroup + "/" + HigressExtVersion

WasmPluginKind = "WasmPlugin"
WasmPluginResource = "wasmplugins"
)

var (
HigressNamespace = DefaultHigressNamespace
WasmPluginGVK = schema.GroupVersionKind{Group: HigressExtGroup, Version: HigressExtVersion, Kind: WasmPluginKind}
WasmPluginGVR = schema.GroupVersionResource{Group: HigressExtGroup, Version: HigressExtVersion, Resource: WasmPluginResource}
)

func AddHigressNamespaceFlags(flags *pflag.FlagSet) {
flags.StringVarP(&HigressNamespace, "namespace", "n",
DefaultHigressNamespace, "Namespace where Higress was installed")
}

type WasmPluginClient struct {
dyn *DynamicClient
}

func NewWasmPluginClient(dynClient *DynamicClient) *WasmPluginClient {
return &WasmPluginClient{dynClient}
}

func (c WasmPluginClient) Get(ctx context.Context, name string) (*unstructured.Unstructured, error) {
return c.dyn.Get(ctx, WasmPluginGVR, HigressNamespace, name)
}

func (c WasmPluginClient) List(ctx context.Context) (*unstructured.UnstructuredList, error) {
return c.dyn.List(ctx, WasmPluginGVR, HigressNamespace)
}

func (c WasmPluginClient) Create(ctx context.Context, obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
return c.dyn.Create(ctx, WasmPluginGVR, HigressNamespace, obj)
}

func (c WasmPluginClient) Delete(ctx context.Context, name string) (*unstructured.Unstructured, error) {
return c.dyn.Delete(ctx, WasmPluginGVR, HigressNamespace, name)
}

func (c WasmPluginClient) Update(ctx context.Context, obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
return c.dyn.Update(ctx, WasmPluginGVR, HigressNamespace, obj)
}

// TODO(WeixinX): Will be changed to WasmPlugin specific Client instead of Unstructured
type DynamicClient struct {
config *rest.Config
client dynamic.Interface
}

func NewDynamicClient(clientConfig clientcmd.ClientConfig) (*DynamicClient, error) {
var (
c DynamicClient
err error
)

c.config, err = clientConfig.ClientConfig()
if err != nil {
return nil, err
}

c.client, err = dynamic.NewForConfig(c.config)
if err != nil {
return nil, err
}

return &c, nil
}

func (c DynamicClient) Get(ctx context.Context, gvr schema.GroupVersionResource, namespace, name string) (*unstructured.Unstructured, error) {
return c.client.Resource(gvr).Namespace(namespace).Get(ctx, name, metav1.GetOptions{})
}

func (c DynamicClient) List(ctx context.Context, gvr schema.GroupVersionResource, namespace string) (*unstructured.UnstructuredList, error) {
return c.client.Resource(gvr).Namespace(namespace).List(ctx, metav1.ListOptions{})
}

func (c DynamicClient) Create(ctx context.Context, gvr schema.GroupVersionResource, namespace string, obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
return c.client.Resource(gvr).Namespace(namespace).Create(ctx, obj, metav1.CreateOptions{})
}

func (c DynamicClient) Delete(ctx context.Context, gvr schema.GroupVersionResource, namespace, name string) (*unstructured.Unstructured, error) {
result, err := c.client.Resource(gvr).Namespace(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return nil, err
}
err = c.client.Resource(gvr).Namespace(namespace).Delete(ctx, name, metav1.DeleteOptions{})
if err != nil {
return nil, err
}
return result, nil
}

func (c DynamicClient) Update(ctx context.Context, gvr schema.GroupVersionResource, namespace string,
obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
return c.client.Resource(gvr).Namespace(namespace).Update(ctx, obj, metav1.UpdateOptions{})
}
Loading