...
1#!/usr/bin/env bash
2
3# Copyright 2020 The Kubernetes Authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# This script generates a jpg image of our internal dependency graph.
18# It relies on go mod and dot to do so and should be run from K8s root.
19# TODO: Containerize the script to remove dependency issues with go mod and dot.
20
21# To generate graph with all Kubernetes modules run
22# ./hack/module-graph.sh
23# To generate graph with just staging modules run
24# ./hack/module-graph.sh staging
25
26error_exit()
27{
28 echo "$1" 1>&2
29 exit 1
30}
31
32staging_dependencies()
33{
34 relevant_modules="(k8s.io/kubernetes|$(find staging/src/k8s.io -maxdepth 1 -mindepth 1 | sed -E 's|staging/src/k8s.io|k8s.io|g' | tr '\n' '|' | sed 's#|$##' ))"
35 # Generating lines of the form " k8s_io_kubernetes -> k8s_io_api"
36 # Use only the directories in staging
37 # Trimming away version info
38 # Replacing non DOT (graph description language) characters with underscores
39 # Dedupe lines
40 # Inserting needed arrow.
41 # Indenting the line appropriately
42 go mod graph | grep -E "^${relevant_modules}(@.*| )${relevant_modules}@.*$" | sed -E 's|@\S+ | |g' | sed -E 's|@\S+$||g' | sed -E 's/[\.\/\-]/_/g' | sort | uniq | sed -E 's| | -> |g' | sed -E 's|^| |g' >> _output/module-dependencies.dot || error_exit "Failed to generate staging dependencies in DOT file"
43}
44
45kubernetes_dependencies()
46{
47 # Generating lines of the form " k8s_io_kubernetes -> k8s_io_api"
48 # Excluding all non Kubernetes dependencies
49 # Trimming away version info
50 # Replacing non DOT (graph description language) characters with underscores
51 # Dedupe lines
52 # Inserting needed arrow.
53 # Indenting the line appropriately
54 go mod graph | grep -E "^.*k8s.io.*k8s.io.*$" | sed -E 's|@\S+ | |g' | sed -E 's|@\S+$||g' | sed -E 's/[\.\/\-]/_/g' | sort | uniq | sed -E 's| | -> |g' | sed -E 's|^| |g' >> _output/module-dependencies.dot || error_exit "Failed to generate kubernetes dependencies in DOT file"
55}
56
57mkdir -p _output
58echo "digraph module_dependencies {" > _output/module-dependencies.dot || error_exit "Failed to open DOT file"
59if [[ -n "$1" && $1 == "staging" ]]; then
60 echo "Generating just staging modules"
61 staging_dependencies
62else
63 echo "Generating all Kubernetes modules"
64 kubernetes_dependencies
65fi
66echo "}" >> _output/module-dependencies.dot || error_exit "Failed to close DOT file"
67dot -Gratio=1,1 -Nwidth=2 -Nheight=2 -Nfontsize=48 -Earrowsize=8 -Tjpg _output/module-dependencies.dot -o _output/module-dependencies.jpg || error_exit "Failed to generate graph from DOT file"
View as plain text