Skip to content

Commit

Permalink
Merge pull request #1083 from CrowleyRajapakse/master
Browse files Browse the repository at this point in the history
Adding graphql API creation
  • Loading branch information
CrowleyRajapakse authored Feb 21, 2024
2 parents 898fc99 + 4d0742a commit 9bf8522
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 8 deletions.
22 changes: 22 additions & 0 deletions apim-apk-agent/internal/k8sClient/k8s_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ func DeployHTTPRouteCR(httpRoute *gwapiv1b1.HTTPRoute, k8sClient client.Client)
}
}

// DeployGQLRouteCR applies the given GqlRoute struct to the Kubernetes cluster.
func DeployGQLRouteCR(gqlRoute *dpv1alpha2.GQLRoute, k8sClient client.Client) {
crGQLRoute := &dpv1alpha2.GQLRoute{}
if err := k8sClient.Get(context.Background(), client.ObjectKey{Namespace: gqlRoute.ObjectMeta.Namespace, Name: gqlRoute.Name}, crGQLRoute); err != nil {
if !k8error.IsNotFound(err) {
loggers.LoggerXds.Error("Unable to get GQLRoute CR: " + err.Error())
}
if err := k8sClient.Create(context.Background(), gqlRoute); err != nil {
loggers.LoggerXds.Error("Unable to create GQLRoute CR: " + err.Error())
} else {
loggers.LoggerXds.Info("GQLRoute CR created: " + gqlRoute.Name)
}
} else {
crGQLRoute.Spec = gqlRoute.Spec
if err := k8sClient.Update(context.Background(), crGQLRoute); err != nil {
loggers.LoggerXds.Error("Unable to update GQLRoute CR: " + err.Error())
} else {
loggers.LoggerXds.Info("GQLRoute CR updated: " + gqlRoute.Name)
}
}
}

// DeploySecretCR applies the given Secret struct to the Kubernetes cluster.
func DeploySecretCR(secret *corev1.Secret, k8sClient client.Client) {
crSecret := &corev1.Secret{}
Expand Down
4 changes: 4 additions & 0 deletions apim-apk-agent/internal/mapper/cr_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func MapAndCreateCR(k8sArtifact transformer.K8sArtifacts, k8sClient client.Clien
httpRoutes.Namespace = namespace
internalk8sClient.DeployHTTPRouteCR(httpRoutes, k8sClient)
}
for _, gqlRoutes := range k8sArtifact.GQLRoutes {
gqlRoutes.Namespace = namespace
internalk8sClient.DeployGQLRouteCR(gqlRoutes, k8sClient)
}
for _, backends := range k8sArtifact.Backends {
backends.Namespace = namespace
internalk8sClient.DeployBackendCR(backends, k8sClient)
Expand Down
2 changes: 1 addition & 1 deletion apim-apk-agent/internal/utils/apis_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func FetchAPIsOnEvent(conf *config.Config, apiUUID *string, k8sClient client.Cli
return nil, err
}
k8ResourceEndpoint := conf.DataPlane.K8ResourceEndpoint
crResponse, err := transformer.GenerateCRs(apkConf, artifact.Swagger, k8ResourceEndpoint)
crResponse, err := transformer.GenerateCRs(apkConf, artifact.Schema, k8ResourceEndpoint)
if err != nil {
logger.LoggerSync.Errorf("Error occured in receiving the updated CRDs: %v", err)
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion apim-apk-agent/pkg/transformer/api_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ type APIArtifact struct {
APIJson string `json:"apiJson"`
APIFileName string `json:"apiFileName"`
EnvConfig string `json:"envConfig"`
Swagger string `json:"swagger"`
Schema string `json:"schema"`
DeploymentDescriptor string `json:"deploymentDescriptor"`
ClientCerts string `json:"clientCert"`
RevisionID uint32 `json:"revisionId"`
Expand Down
1 change: 1 addition & 0 deletions apim-apk-agent/pkg/transformer/k8s_artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type K8sArtifacts struct {
API dpv1alpha2.API
HTTPRoutes map[string]*gwapiv1b1.HTTPRoute
GQLRoutes map[string]*dpv1alpha2.GQLRoute
Backends map[string]*v1alpha1.Backend
Scopes map[string]*v1alpha1.Scope
Authentication map[string]*dpv1alpha2.Authentication
Expand Down
17 changes: 14 additions & 3 deletions apim-apk-agent/pkg/transformer/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ func getAPIType(protocolType string) string {
switch protocolType {
case "HTTP", "HTTPS":
apiType = "REST"
case "GraphQL":
apiType = "GraphQL"
case "GRAPHQL":
apiType = "GRAPHQL"
}
return apiType
}
Expand Down Expand Up @@ -238,7 +238,7 @@ func mapAuthConfigs(authHeader string, secSchemes []string, certAvailable bool,
// GenerateCRs takes the .apk-conf, api definition, vHost and the organization for a particular API and then generate and returns
// the relavant CRD set as a zip
func GenerateCRs(apkConf string, apiDefinition string, k8ResourceGenEndpoint string) (*K8sArtifacts, error) {
k8sArtifact := K8sArtifacts{HTTPRoutes: make(map[string]*gwapiv1b1.HTTPRoute), Backends: make(map[string]*dpv1alpha1.Backend), Scopes: make(map[string]*dpv1alpha1.Scope), Authentication: make(map[string]*dpv1alpha2.Authentication), APIPolicies: make(map[string]*dpv1alpha2.APIPolicy), InterceptorServices: make(map[string]*dpv1alpha1.InterceptorService), ConfigMaps: make(map[string]*corev1.ConfigMap), Secrets: make(map[string]*corev1.Secret), RateLimitPolicies: make(map[string]*dpv1alpha1.RateLimitPolicy)}
k8sArtifact := K8sArtifacts{HTTPRoutes: make(map[string]*gwapiv1b1.HTTPRoute), GQLRoutes: make(map[string]*dpv1alpha2.GQLRoute), Backends: make(map[string]*dpv1alpha1.Backend), Scopes: make(map[string]*dpv1alpha1.Scope), Authentication: make(map[string]*dpv1alpha2.Authentication), APIPolicies: make(map[string]*dpv1alpha2.APIPolicy), InterceptorServices: make(map[string]*dpv1alpha1.InterceptorService), ConfigMaps: make(map[string]*corev1.ConfigMap), Secrets: make(map[string]*corev1.Secret), RateLimitPolicies: make(map[string]*dpv1alpha1.RateLimitPolicy)}
if apkConf == "" {
logger.LoggerTransformer.Error("Empty apk-conf parameter provided. Unable to generate CRDs.")
return nil, errors.New("Error: APK-Conf can't be empty")
Expand Down Expand Up @@ -429,6 +429,14 @@ func GenerateCRs(apkConf string, apiDefinition string, k8ResourceGenEndpoint str
continue
}
k8sArtifact.Secrets[secret.Name] = &secret
case "GQLRoute":
var gqlRoute dpv1alpha2.GQLRoute
err = k8Yaml.Unmarshal(yamlData, &gqlRoute)
if err != nil {
logger.LoggerSync.Errorf("Error unmarshaling GQLRoute YAML: %v", err)
continue
}
k8sArtifact.GQLRoutes[gqlRoute.Name] = &gqlRoute
default:
logger.LoggerSync.Errorf("[!]Unknown Kind parsed from the YAML File: %v", kind)
}
Expand Down Expand Up @@ -523,6 +531,9 @@ func addOrganization(k8sArtifact *K8sArtifacts, organization string) {
for _, httproutes := range k8sArtifact.HTTPRoutes {
httproutes.ObjectMeta.Labels[k8sOrganizationField] = organizationHash
}
for _, gqlroutes := range k8sArtifact.GQLRoutes {
gqlroutes.ObjectMeta.Labels[k8sOrganizationField] = organizationHash
}
for _, authentication := range k8sArtifact.Authentication {
authentication.ObjectMeta.Labels[k8sOrganizationField] = organizationHash
}
Expand Down
17 changes: 14 additions & 3 deletions apim-apk-agent/pkg/transformer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@ func readZipFile(file *zip.File) (*APIArtifact, error) {
if err != nil {
return nil, err
}
apiArtifact.Swagger = string(openAPIContent)
apiArtifact.Schema = string(openAPIContent)
}

if strings.Contains(file.Name, "schema.graphql") {
graphqlContent, err := ReadContent(file)
if err != nil {
return nil, err
}
apiArtifact.Schema = string(graphqlContent)
}

if strings.Contains(file.Name, "api.json") {
Expand Down Expand Up @@ -118,8 +126,11 @@ func readAPIZipFile(file *zip.File, apiArtifact *APIArtifact) error {
}

if strings.Contains(file.Name, "swagger.json") {
apiArtifact.Swagger = string(content)
apiArtifact.Schema = string(content)
}

if strings.Contains(file.Name, "schema.graphql") {
apiArtifact.Schema = string(content)
}

if strings.Contains(file.Name, "api.json") {
Expand Down Expand Up @@ -215,7 +226,7 @@ func DecodeAPIArtifacts(payload []byte) ([]APIArtifact, error) {
apiArtifacts = append(apiArtifacts, apiArtifact)
}
apiArtifact.APIJson = ""
apiArtifact.Swagger = ""
apiArtifact.Schema = ""
apiArtifact.RevisionID = 0
apiArtifact.APIFileName = ""
}
Expand Down

0 comments on commit 9bf8522

Please sign in to comment.