Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #93 from weaveworks/storage_client
Browse files Browse the repository at this point in the history
Serializer, Storage and Client implementations
  • Loading branch information
luxas committed Jul 3, 2019
2 parents 756beef + 0b9cb5d commit 7bba07f
Show file tree
Hide file tree
Showing 26 changed files with 1,302 additions and 61 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ tidy:
go mod vendor
gofmt -s -w pkg cmd
goimports -w pkg cmd
hack/generate-client.sh
go run hack/cobra.go

shell:
Expand Down
2 changes: 1 addition & 1 deletion cmd/ignite/run/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (cf *CreateFlags) parseArgsAndConfig(args []string) error {
if len(cf.ConfigFile) != 0 {
// Marshal into a "clean" object, discard all flag input
cf.VM = &v1alpha1.VM{}
if err := scheme.DecodeFileInto(cf.ConfigFile, cf.VM); err != nil {
if err := scheme.Serializer.DecodeFileInto(cf.ConfigFile, cf.VM); err != nil {
return err
}
}
Expand Down
13 changes: 4 additions & 9 deletions docs/cli/ignite_image_import.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ Import a new base image for VMs
Import a new base image for VMs, takes in a Docker image as the source.
The base image is an ext4 block device file, which contains a root filesystem.

If the import kernel flag (-k, --import-kernel) is specified,
/boot/vmlinux is extracted from the image and added to a new
VM kernel object named after the flag.
If a kernel is found in the image, /boot/vmlinux is extracted from it
and imported to a kernel with the same name.

Example usage:
$ ignite build luxas/ubuntu-base:18.04 \
--name my-image \
--import-kernel my-kernel
$ ignite image import luxas/ubuntu-base:18.04


```
Expand All @@ -25,9 +22,7 @@ ignite image import <source> [flags]
### Options

```
-h, --help help for import
-k, --import-kernel string Import a new kernel from /boot/vmlinux in the image with the specified name
-n, --name string Specify the name
-h, --help help for import
```

### Options inherited from parent commands
Expand Down
12 changes: 12 additions & 0 deletions hack/generate-client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

SCRIPT_DIR=$( dirname "${BASH_SOURCE[0]}" )
cd ${SCRIPT_DIR}/..

Resources="VM Image Kernel"
for Resource in ${Resources}; do
resource=$(echo "${Resource}" | awk '{print tolower($0)}')
sed -e "s|Resource|${Resource}|g;s|resource|${resource}|g;/build ignore/d" \
pkg/client/client_resource_template.go > \
pkg/client/zz_generated.client_${resource}.go
done
41 changes: 8 additions & 33 deletions pkg/apis/ignite/scheme/scheme.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package scheme

import (
"io/ioutil"

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
k8sserializer "k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"

"github.com/weaveworks/ignite/pkg/apis/ignite/v1alpha1"
"github.com/weaveworks/ignite/pkg/storage/serializer"
)

var (
// Scheme is the runtime.Scheme to which all types are registered.
Scheme = runtime.NewScheme()

// Codecs provides access to encoding and decoding for the scheme.
Codecs = serializer.NewCodecFactory(Scheme)
// codecs provides access to encoding and decoding for the scheme.
// codecs is private, as Serializer will be used for all higher-level encoding/decoding
codecs = k8sserializer.NewCodecFactory(Scheme)

// Serializer provides high-level encoding/decoding functions
Serializer = serializer.NewSerializer(Scheme, &codecs)
)

func init() {
Expand All @@ -28,30 +30,3 @@ func AddToScheme(scheme *runtime.Scheme) {
utilruntime.Must(v1alpha1.AddToScheme(Scheme))
utilruntime.Must(scheme.SetVersionPriority(v1alpha1.SchemeGroupVersion))
}

// DecodeFileInto takes a file path and a target object to serialize the data into
func DecodeFileInto(filePath string, obj runtime.Object) error {
content, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}

return DecodeInto(content, obj)
}

// DecodeInto takes byte content and a target object to serialize the data into
func DecodeInto(content []byte, obj runtime.Object) error {
return runtime.DecodeInto(Codecs.UniversalDecoder(), content, obj)
}

// EncodeYAML encodes the specified object for a specific version to YAML bytes
func EncodeYAML(obj runtime.Object, groupVersion schema.GroupVersion) ([]byte, error) {
serializerInfo, _ := runtime.SerializerInfoForMediaType(Codecs.SupportedMediaTypes(), runtime.ContentTypeYAML)
return runtime.Encode(Codecs.EncoderForVersion(serializerInfo.Serializer, groupVersion), obj)
}

// EncodeYAML encodes the specified object for a specific version to pretty JSON bytes
func EncodeJSON(obj runtime.Object, groupVersion schema.GroupVersion) ([]byte, error) {
serializerInfo, _ := runtime.SerializerInfoForMediaType(Codecs.SupportedMediaTypes(), runtime.ContentTypeJSON)
return runtime.Encode(Codecs.EncoderForVersion(serializerInfo.PrettySerializer, groupVersion), obj)
}
27 changes: 25 additions & 2 deletions pkg/apis/ignite/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,29 @@ var (
AddToScheme = localSchemeBuilder.AddToScheme
)

// GroupName is the group name use in this package
const GroupName = "ignite.weave.works"
const (
// GroupName is the group name use in this package
GroupName = "ignite.weave.works"

// VMKind returns the kind for the VM API type
VMKind = "VM"
// KernelKind returns the kind for the Kernel API type
KernelKind = "Kernel"
// PoolKind returns the kind for the Pool API type
PoolKind = "Pool"
// ImageKind returns the kind for the Image API type
ImageKind = "Image"
)

// SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = schema.GroupVersion{
Group: GroupName,
Version: "v1alpha1",
}
var internalGV = schema.GroupVersion{
Group: GroupName,
Version: runtime.APIVersionInternal,
}

// Adds the list of known types to the given scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
Expand All @@ -33,6 +48,14 @@ func addKnownTypes(scheme *runtime.Scheme) error {
&Pool{},
&Image{},
)
// TODO: This is a hack, but for now it's sufficient.
// Eventually, we should break this out to a real internal API package
scheme.AddKnownTypes(internalGV,
&VM{},
&Kernel{},
&Pool{},
&Image{},
)

return nil
}
6 changes: 3 additions & 3 deletions pkg/apis/ignite/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 34 additions & 4 deletions pkg/apis/meta/v1alpha1/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/c2h5oh/datasize"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
)
Expand All @@ -14,25 +15,54 @@ const (
SectorSize = 512
)

// APIType is a struct implementing Object, used for
// unmarshalling unknown objects into this intermediate type
// where .Name, .UID, .Kind and .APIVersion become easily available
type APIType struct {
metav1.TypeMeta `json:",inline"`
ObjectMeta `json:"metadata"`
}

// APITypeList is a list of many pointers APIType objects
type APITypeList []*APIType

// ObjectMeta have to be embedded into any serializable object.
// It provides the .GetName() and .GetUID() methods that help
// implement the Object interface
type ObjectMeta struct {
Name string `json:"name"`
UID types.UID `json:"uid,omitempty"`
Name string `json:"name"`
UID types.UID `json:"uid,omitempty"`
Created *metav1.Time `json:"created,omitempty"`
}

// GetName returns the name of the Object
func (o *ObjectMeta) GetName() string {
return o.Name
}

// GetUID returns the UID of the Object
func (o *ObjectMeta) GetUID() types.UID {
return o.UID
}

// All types implementing Object conform to this
// interface, it's mainly used for filtering
// GetCreated returns when the Object was created
func (o *ObjectMeta) GetCreated() *metav1.Time {
return o.Created
}

// SetCreated returns when the Object was created
func (o *ObjectMeta) SetCreated(t *metav1.Time) {
o.Created = t
}

// Object extends k8s.io/apimachinery's runtime.Object with
// extra GetName() and GetUID() methods from ObjectMeta
type Object interface {
runtime.Object
GetName() string
GetUID() types.UID
GetCreated() *metav1.Time
SetCreated(t *metav1.Time)
}

// Size specifies a common unit for data sizes
Expand Down
48 changes: 48 additions & 0 deletions pkg/apis/meta/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions pkg/client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
## client

Package client is a Go client for Ignite resources.

For example, to list running VMs (the equivalent of "ignite vm ls"), and update the
VM with a new IP address:

```go
package main
import (
"context"
"fmt"
"net"
"github.com/weaveworks/ignite/pkg/client"
)
func main() {
// List VMs managed by Ignite
vmList, err := client.VMs().List()
if err != nil {
panic(err)
}
for _, vm := range vmList {
// Modify the object
vm.Status.IPAddresses = append(vm.Status.IPAddresses, net.IP{127,0,0,1})
// Save the new VM state
if err := client.VMs().Set(vm); err != nil {
panic(err)
}
}

// Get a specific image, and print its size
myImage, err := client.Images().Get("my-image")
if err != nil {
panic(err)
}
fmt.Printf("Image my-vm size: %s\n", myImage.Spec.Source.Size.String())

// Use the dynamic client
myVM, err := client.Dynamic("VM").Get("my-vm")
if err != nil {
panic(err)
}
fmt.Printf("VM my-vm cpus: %d\n", myVM.Spec.CPUs)
}
```
Loading

0 comments on commit 7bba07f

Please sign in to comment.