Skip to content

Commit

Permalink
Merge pull request #235 from Revolyssup/abc
Browse files Browse the repository at this point in the history
Add static and dynamic comp gen via new method
  • Loading branch information
leecalcote authored Jan 24, 2022
2 parents 5da205e + b8073c0 commit 150006c
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 213 deletions.
116 changes: 20 additions & 96 deletions .github/workflows/update-oam-defs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,118 +3,42 @@ on:
push:
branches:
- 'master'

schedule:
- cron: "0 0 * * *"
jobs:
check_consul_version:
name: Check Consul Version
GenerateComponents:
runs-on: ubuntu-latest
outputs:
NEW_CONSUL_VERSION: ${{ env.NEW_CONSUL_VERSION }}
NEW_CONSUL_CHART_VERSION: ${{ env.NEW_CONSUL_CHART_VERSION }}
EXISTS: ${{ env.EXISTS }}
steps:
- name: Setup Helm
id: sh
uses: azure/setup-helm@v1

# retrieves consul version from helm charts
- name: Get Consul Latest Release Metadata
id: glrt
run:
helm repo add hashicorp https://helm.releases.hashicorp.com;
helm repo update;
RELEASE_METADATA=$(helm search repo hashicorp/consul --versions | sed -n 2p$'\n' | sed -e 's/\s/;/g' | tr -s ';');
echo "NEW_CONSUL_CHART_VERSION=`echo $RELEASE_METADATA | cut -d ';' -f2`" >> $GITHUB_ENV;
echo "NEW_CONSUL_VERSION=`echo $RELEASE_METADATA | cut -d ';' -f3`" >> $GITHUB_ENV;

- name: Check out code
uses: actions/checkout@master
with:
fetch-depth: 1

- name: Check if manifests for latest release exist
run: |
if [ -d templates/oam/workloads/${{ env.NEW_CONSUL_VERSION }} ]
then
echo "EXISTS=true" >> $GITHUB_ENV
else
export "EXISTS=false" >> $GITHUB_ENV
fi
generator:
name: Generate and push OAM definitions
needs: [ check_consul_version ]
if: ${{ !needs.check_consul_version.outputs.EXISTS }}
env:
NEW_CONSUL_VERSION: ${{ needs.check_consul_version.outputs.NEW_CONSUL_VERSION }}
NEW_CONSUL_CHART_VERSION: ${{ needs.check_consul_version.outputs.NEW_CONSUL_CHART_VERSION }}
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@master
token: ${{ secrets.GH_ACCESS_TOKEN }}
ref: "master"
- name: Setup Go
uses: actions/setup-go@v1
with:
fetch-depth: 1
token: ${{secrets.GH_ACCESS_TOKEN}}

- name: Get Consul manifests
run: |
mkdir ./consul-crds
cd ./consul-crds
curl --silent --remote-name-all `curl --silent -H "Accept: application/vnd.github.VERSION.raw" https://api.github.com/repos/hashicorp/consul-k8s/contents/control-plane/config/crd/bases\?ref\=$(printf v%s $NEW_CONSUL_CHART_VERSION) | jq 'map(.download_url) | .[]' | tr -d '"'`;
cd ..
awk 'FNR==1 && NR>1 { printf("\n%s\n\n","---") } 1' consul-crds/*.yaml > ~/consul.yaml
- name: Build jsonschema util
go-version: 1.17
- name: Run adapter to create components
run: |
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
nvm install v14.15.3
nvm install-latest-npm
git clone https://github.com/layer5io/kubeopenapi-jsonschema util
cd util
npm i; npm i nexe -g
make linux
- name: Generate Workload definitions
run: |
dirPath=../templates/oam/workloads/$NEW_CONSUL_VERSION
cd util
mkdir -p $dirPath
meshName=Consul
smpMeshName=CONSUL
template='{"apiVersion":"core.oam.dev/v1alpha1","kind":"WorkloadDefinition","metadata":{},"spec":{"definitionRef":{},"metadata":{"@type":"pattern.meshery.io/mesh/workload","meshVersion":"'v$NEW_CONSUL_VERSION'","meshName":"'$smpMeshName'","k8sAPIVersion":null,"k8sKind":""}}}'
crds=$(./kubeopenapi-jsonschema --location ~/consul.yaml -t yaml --filter '$[?(@.kind=="CustomResourceDefinition")]' -o json --o-filter '$..["spec"]["names"]["kind"]' | jq '.[]')
for t in ${crds[@]}; do
nameUpper=`echo $t | tr -d '"'`
nameLower=`echo $t | tr -d '"' | tr '[:upper:]' '[:lower:]'`
definitionRef=$(printf %s.consul.meshery.layer5.io $nameLower)
apiVersion=$(./kubeopenapi-jsonschema --location ~/consul.yaml -t yaml --filter '$[?(@.kind=="CustomResourceDefinition" && @.spec.names.kind=='$t')]..spec.versions[0]' --o-filter "$[].name" -o json | jq '.[]' | tr -d '"')
apiGroup=$(./kubeopenapi-jsonschema --location ~/consul.yaml -t yaml --filter '$[?(@.kind=="CustomResourceDefinition" && @.spec.names.kind=='$t')]..spec' --o-filter "$[].group" -o json | jq '.[]' | tr -d '"')
./kubeopenapi-jsonschema --location ~/consul.yaml -t yaml --filter '$[?(@.kind=="CustomResourceDefinition" && @.spec.names.kind=='$t')]..openAPIV3Schema.properties.spec' --o-filter "$[]" -o json |\
jq '.[]' > $dirPath/$nameLower.consul.meshery.layer5io.schema.json
echo $template |\
jq '
."metadata"."name" = "'$(printf %s.Consul $nameUpper)'"
| ."spec"."metadata"."k8sAPIVersion" = "'$(printf $apiGroup/$apiVersion $apiGroup $apiVersion)'"
| ."spec"."metadata"."k8sKind" = "'$nameUpper'"
| ."spec"."definitionRef"."name"="'$definitionRef'"' > $dirPath/$nameLower.consul_definition.json
touch log.txt
mkdir -p ~/.meshery/bin
(FORCE_DYNAMIC_REG=true DEBUG=true go run main.go > log.txt 2>&1) &
end=$((SECONDS+300))
while [ $SECONDS -lt $end ]; do
if [ "$(cat log.txt | grep "Component Creation completed for version")" != "" ];then
echo "New components created"
break
fi
done
- name: Cleanup
run: |
rm -rf util
rm log.txt
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
file_pattern: templates/
commit_user_name: l5io
commit_user_email: [email protected]
commit_author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>
commit_options: '--signoff'
commit_options: "--signoff"
commit_message: "[Patterns] Pattern components generated from latest Consul manifests"
branch: master
57 changes: 57 additions & 0 deletions build/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package build

import (
"os"
"path/filepath"
"strings"

"github.com/layer5io/meshery-adapter-library/adapter"
"github.com/layer5io/meshery-consul/internal/config"

"github.com/layer5io/meshkit/utils"
"github.com/layer5io/meshkit/utils/manifests"
smp "github.com/layer5io/service-mesh-performance/spec"
)

var DefaultGenerationMethod string
var LatestVersion string
var WorkloadPath string
var CRDnames []string
var OverrideURL string
var AllVersions []string

//NewConfig creates the configuration for creating components
func NewConfig(version string) manifests.Config {
return manifests.Config{
Name: smp.ServiceMesh_Type_name[int32(smp.ServiceMesh_CONSUL)],
MeshVersion: version,
Filter: manifests.CrdFilter{
RootFilter: []string{"$[?(@.kind==\"CustomResourceDefinition\")]"},
NameFilter: []string{"$..[\"spec\"][\"names\"][\"kind\"]"},
VersionFilter: []string{"$[0]..spec.versions[0]"},
GroupFilter: []string{"$[0]..spec"},
SpecFilter: []string{"$[0]..openAPIV3Schema.properties.spec"},
ItrFilter: []string{"$[?(@.spec.names.kind"},
ItrSpecFilter: []string{"$[?(@.spec.names.kind"},
VField: "name",
GField: "group",
},
}
}
func GetDefaultURL(crd string) string {
if OverrideURL != "" {
return OverrideURL
}
return strings.Join([]string{"https://raw.githubusercontent.com/hashicorp/consul-k8s/main/control-plane/config/crd/bases", crd}, "/")
}
func init() {
wd, _ := os.Getwd()
WorkloadPath = filepath.Join(wd, "templates", "oam", "workloads")
AllVersions, _ = utils.GetLatestReleaseTagsSorted("hashicorp", "consul-k8s")
if len(AllVersions) == 0 {
return
}
CRDnames, _ = config.GetFileNames("hashicorp", "consul-k8s", "control-plane/config/crd/bases/")
LatestVersion = AllVersions[len(AllVersions)-1]
DefaultGenerationMethod = adapter.Manifests
}
19 changes: 13 additions & 6 deletions consul/oam/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

var (
basePath, _ = os.Getwd()
workloadPath = filepath.Join(basePath, "templates", "oam", "workloads")
WorkloadPath = filepath.Join(basePath, "templates", "oam", "workloads")
traitPath = filepath.Join(basePath, "templates", "oam", "traits")
)

Expand All @@ -22,18 +22,16 @@ type schemaDefinitionPathSet struct {
name string
}

var AvailableVersions = map[string]bool{}
var pathSets []schemaDefinitionPathSet

// RegisterWorkloads will register all of the workload definitions
// present in the path oam/workloads
//
// Registration process will send POST request to $runtime/api/oam/workload
func RegisterWorkloads(runtime, host string) error {
oamRDP := []adapter.OAMRegistrantDefinitionPath{}

pathSets, err := load(workloadPath)
if err != nil {
return ErrLoadingPathset(err)
}

for _, pathSet := range pathSets {
metadata := map[string]string{
config.OAMAdapterNameMetadataKey: config.ConsulOperation,
Expand Down Expand Up @@ -108,6 +106,7 @@ func load(basePath string) ([]schemaDefinitionPathSet, error) {
jsonSchemaPath: fmt.Sprintf("%s.meshery.layer5io.schema.json", nameWithPath),
name: filepath.Base(nameWithPath),
})
AvailableVersions[filepath.Base(filepath.Dir(path))] = true
}

return nil
Expand All @@ -117,3 +116,11 @@ func load(basePath string) ([]schemaDefinitionPathSet, error) {

return res, nil
}

func init() {
var err error
pathSets, err = load(WorkloadPath)
if err != nil {
fmt.Println("Error while loading components: ", err.Error())
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ replace (
)

require (
github.com/layer5io/meshery-adapter-library v0.1.25
github.com/layer5io/meshkit v0.5.0
github.com/layer5io/meshery-adapter-library v0.5.2
github.com/layer5io/meshkit v0.5.2
github.com/layer5io/service-mesh-performance v0.3.3
gopkg.in/yaml.v2 v2.4.0
)
Loading

0 comments on commit 150006c

Please sign in to comment.