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

docs(policy): automatically convert policy yaml #1290

Merged
merged 1 commit into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
86 changes: 4 additions & 82 deletions app/_src/policies/meshcircuitbreaker.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,36 +441,7 @@ spec:

#### Basic circuit breaker for outbound traffic from web, to backend service

{% tabs usage useUrlFragment=false %}
{% tab usage Kubernetes %}

```yaml
apiVersion: kuma.io/v1alpha1
kind: MeshCircuitBreaker
metadata:
name: web-to-backend-circuit-breaker
namespace: {{site.mesh_namespace}}
spec:
targetRef:
kind: MeshService
name: web
to:
- targetRef:
kind: MeshService
name: backend
default:
connectionLimits:
maxConnections: 2
maxPendingRequests: 8
maxRetries: 2
maxRequests: 2
```

We will apply the configuration with `kubectl apply -f [..]`.
{% endtab %}

{% tab usage Universal %}

{% policy_yaml usage %}
```yaml
type: MeshCircuitBreaker
name: web-to-backend-circuit-breaker
Expand All @@ -490,57 +461,11 @@ spec:
maxRetries: 2
maxRequests: 2
```

We will apply the configuration with `kumactl apply -f [..]` or via the [HTTP API](/docs/{{ page.version }}/reference/http-api).
{% endtab %}
{% endtabs %}
{% endpolicy_yaml %}

#### Outlier detection for inbound traffic to backend service

{% tabs protocol useUrlFragment=false %}
{% tab protocol Kubernetes %}

```yaml
apiVersion: kuma.io/v1alpha1
kind: MeshCircuitBreaker
metadata:
name: backend-inbound-outlier-detection
namespace: {{site.mesh_namespace}}
spec:
targetRef:
kind: MeshService
name: web
from:
- targetRef:
kind: Mesh
default:
outlierDetection:
interval: 5s
baseEjectionTime: 30s
maxEjectionPercent: 20
splitExternalAndLocalErrors: true
detectors:
totalFailures:
consecutive: 10
gatewayFailures:
consecutive: 10
localOriginFailures:
consecutive: 10
successRate:
minimumHosts: 5
requestVolume: 10
standardDeviationFactor: 1.9
failurePercentage:
requestVolume: 10
minimumHosts: 5
threshold: 85
```

We will apply the configuration with `kubectl apply -f [..]`.
{% endtab %}

{% tab protocol Universal %}

{% policy_yaml protocol %}
```yaml
type: MeshCircuitBreaker
name: backend-inbound-outlier-detection
Expand Down Expand Up @@ -574,10 +499,7 @@ spec:
minimumHosts: 5
threshold: 85
```

We will apply the configuration with `kumactl apply -f [..]` or via the [HTTP API](/docs/{{ page.version }}/reference/http-api).
{% endtab %}
{% endtabs %}
{% endpolicy_yaml %}

## All policy options

Expand Down
1 change: 1 addition & 0 deletions jekyll-kuma-plugins/lib/jekyll/kuma-plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
require_relative "kuma-plugins/version"
require_relative 'kuma-plugins/liquid/tags/test'
require_relative 'kuma-plugins/liquid/tags/policyschema'
require_relative 'kuma-plugins/liquid/tags/policyyaml'
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# This plugins lets us to write the policy YAML only once.
# It removes duplication of examples for both universal and kubernetes environments.
# The expected format is universal. It only works for policies V2 with a `spec` blocks.
require 'yaml'
module Jekyll
module KumaPlugins
module Liquid
module Tags
class PolicyYaml < ::Liquid::Block
def initialize(tag_name, tabs_name, options)
super
@tabs_name = tabs_name
end

def render(context)
content = super
return "" unless content != ""
site_data = context.registers[:site].config
mesh_namespace = site_data['mesh_namespace']
# remove ```yaml header and ``` footer
pure_yaml = content.gsub(/`{3}yaml\n/, '').gsub(/`{3}/, '')
yaml_data = YAML.load(pure_yaml)
kube_hash = {
"apiVersion" => "kuma.io/v1alpha1",
"kind" => yaml_data["type"],
"metadata" => {
"name" => yaml_data["name"],
"namespace" => mesh_namespace,
"labels" => {
"kuma.io/mesh" => yaml_data["mesh"]
}
},
"spec" => yaml_data["spec"]
}
# remove `---` header and end line generated by YAML.dump
kube_yaml = YAML.dump(kube_hash).gsub(/^---\n/, '').chomp

htmlContent = "
{% tabs #{@tabs_name} useUrlFragment=false %}
{% tab #{@tabs_name} Kubernetes %}
```yaml
#{kube_yaml}
```
{% endtab %}
{% tab #{@tabs_name} Universal %}
#{content}
{% endtab %}
{% endtabs %}"
::Liquid::Template.parse(htmlContent).render(context)
end
end
end
end
end
end

Liquid::Template.register_tag('policy_yaml', Jekyll::KumaPlugins::Liquid::Tags::PolicyYaml)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
RSpec.describe Jekyll::KumaPlugins::Liquid::Tags::PolicyYaml do
subject { described_class.parse('policy_yaml', "", Liquid::Tokenizer.new(entry + '{%endpolicy_yaml%}'), Liquid::ParseContext.new).render(Liquid::Context.new({
registers: {
:site => {
config: {}
}
}
}))}

context "with nothing" do
let(:entry) {''}
it "is empty" do
expect(subject).to eq('')
end
end
end