<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alexander @ Karagatan on Medium]]></title>
        <description><![CDATA[Stories by Alexander @ Karagatan on Medium]]></description>
        <link>https://medium.com/@karagatan?source=rss-bc74cf7fc2b4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*efTXuoxybrYg0RTDpqcW2g.png</url>
            <title>Stories by Alexander @ Karagatan on Medium</title>
            <link>https://medium.com/@karagatan?source=rss-bc74cf7fc2b4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 18:22:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@karagatan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Mailnite — New Mail Server]]></title>
            <link>https://medium.com/@karagatan/mailnite-new-mail-server-6749c53459de?source=rss-bc74cf7fc2b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/6749c53459de</guid>
            <category><![CDATA[mailnite]]></category>
            <dc:creator><![CDATA[Alexander @ Karagatan]]></dc:creator>
            <pubDate>Fri, 14 Aug 2026 02:40:41 GMT</pubDate>
            <atom:updated>2026-08-14T02:40:41.965Z</atom:updated>
            <content:encoded><![CDATA[<p>Today, I am anoncing the new mail server for personal and corporate use</p><p>In 2019 I opened for myself a nice game called Fortnite. I plated day after day for many years and became pretty much constant 1–4 place winner. I liked this so much so decided to write my own mail server and call is Mailnite.</p><p>Many years I wrote it peace by peace and finally this summer I achieved pretty much stable version of something usable for my opinion, that I would like to present today.</p><p>Welcome, here is the</p><p><a href="https://mailnite.com/">Mailnite - The Protected Home Mail Server</a></p><p>This new mail server was created because I do not really trust the existing mail infrastructure. All mails travel unencrypted, and only protected by TLS certificates, whereas there is no enforced protection on TLS on 25 port (SkipVerification), that is kind of optional makes all those things totally unprotected.</p><p>I was thinking between compromise of convinience and security. S/MIME and PGP protocols are perfect, but useless, you can not index those emails, you can not use them in AI and pipelines. I wanted to keep general purpose infrastructure with close to P2P mail protection using Public/Private key cryptography.</p><p>Mailnite is my try to solve this on domain level. Each domain has public key, but it is not a DKIM protocol using only for signature, I am suggesting a new protocol MKDP (MailKey) that is using for the encryption of subject and body of the email.</p><p>Every time when you write the email to recipient that has MailKey installed on their domain, we check first the DNS record. If we found _mailkey.{domain} DNS record then we deal with MailKey compatible mail server. In this case we can ask the server by HTTPS protocol for the public key and use it for the encryption.</p><p>We are sending the email encrypted to the 25 relay even if we use TLS additionally on top of it. Those things are using for different purpose and mutually protects each other. When recipient receives the email, the first action that he do is checking the special Header, that indicates that email was encrypted by MailKey and decrypts it. In this case sender and receiver do not do any additional action to encrypt and decrypt emails, but they can observe in WebMail special indicator that tells if email was protected or not.</p><p>Going forward, I would say that mailnite.com server built as shareware sfotware, you can install the full functional server and even without license it would provide the same functionality as with license, it is done becase email infrastructure is extreamly important, the only difference is small marketing suffix in mail body that it was sent by mailnite.com</p><p>All incoming emails are stored in encrypted local database, for the corporate installation we have Helm chart for Kubernetes and external Key-Value database supported for this called ConsensysDB.</p><p>Mailnite has integrations with different AI providers: Ollama, OpenAI, Claude. In the current version v1.1.0 we support the AI providers for email summarizations, classification to categories, simplification, recognition.</p><p>In the future versions I am planing to add clear Machine Learning models for spam recognition and classification, since they are working way better than the general purpose foundational models.</p><p>Installation of mailnite is extreamly simple. You can install in 3 possible ways:</p><ol><li>Home lab server under the NAT. In this case you need to purchase entry level VDS with public IP or get is somewhere for free. 25 mail port should be open inbound and outbound on it. You are not installing the mailnite on the VDS, you are installing only mailrelay, very small open source solution to open and listen ports and connect with each other.</li><li>Full functioning VDS server (or even dedicated server) with public IP. In this case you install mailnite as application, allow to listen ports less than 1024 with special settings and enjoy full functional server in one place. Extreamly easy setup. That could be any virtual machine with public IP from AWS, GCP, Oracle or any other cloud or hosting provider.</li><li>Kubernetes environment. This installation I am using in mailnite.com. It could be with one statefullset pod or it could have ConsensusDB, you also could use external mailrelay or forward all ports rto PortNodes. Depending on your needs, but I found that K8s with MailRelay is the easiest combination and I can run in cluster multiple mail servers.</li></ol><p>Please use the mailnite.com to create demo accounts and test it, this is for free.</p><p>Keep updated.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6749c53459de" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building llama.cpp for MacBook M4]]></title>
            <link>https://medium.com/@karagatan/building-llama-cpp-for-macbook-m4-30e1a5c73e46?source=rss-bc74cf7fc2b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/30e1a5c73e46</guid>
            <category><![CDATA[llama-cpp]]></category>
            <dc:creator><![CDATA[Alexander @ Karagatan]]></dc:creator>
            <pubDate>Fri, 06 Mar 2026 14:49:11 GMT</pubDate>
            <atom:updated>2026-03-06T14:49:11.187Z</atom:updated>
            <content:encoded><![CDATA[<p>Running local models on hardware in MacOs</p><p>I have got a new MacbookPro on M4 chip with 24Gb memory. And the first thing that I would like to do on it is to test content generation performance.</p><p>First of all, I downloaded distributive from llama.cpp directly but MacOs asked permission to run on every file, on every library and finally crashed.</p><p>After that I learned that in MacOs the best software if you built it there.</p><p>Therefore, I decided to build llama.cpp and run gpt-oss:20b model on it.</p><p>First of all let’s check if our laptop supports Metal.</p><pre>system_profiler SPDisplaysDataType</pre><p>After running this command I am getting this output</p><pre>Graphics/Displays:<br><br>    Apple M4:<br><br>      Chipset Model: Apple M4<br>      Type: GPU<br>      Bus: Built-In<br>      Total Number of Cores: 10<br>      Vendor: Apple (0x106b)<br>      Metal Support: Metal 3<br>      Displays:<br>        Color LCD:<br>          Display Type: Built-in Liquid Retina XDR Display<br>          Resolution: 3024 x 1964 Retina<br>          Main Display: Yes<br>          Mirror: Off<br>          Online: Yes<br>          Automatically Adjust Brightness: Yes<br>          Connection Type: Internal<br>        LG HDR 4K:<br>          Resolution: 3840 x 2160 (2160p/4K UHD 1 - Ultra High Definition)<br>          UI Looks like: 1920 x 1080 @ 60.00Hz<br>          Mirror: Off<br>          Online: Yes<br>          Rotation: Supported</pre><p>Looks good, we have Metal 3</p><p>Next, we need to install some build packages</p><pre>brew install cmake ninja git</pre><p>After installing let’s use git to clone the project</p><pre>git clone https://github.com/ggml-org/llama.cpp<br>cd llama.cpp</pre><p>And build it</p><pre>cmake -B build -G Ninja -DGGML_METAL=ON<br>cmake --build build --config Release -j 8</pre><p>Then we need to run local llama.cpp</p><pre>./build/bin/llama-cli --gpt-oss-20b-default -ngl 999 -p “test”</pre><p>The param -nlg 999 is using to define number of DNN layers that we place in GPU, so in our case we max them.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=30e1a5c73e46" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Comparision of LLM models for coding and planing]]></title>
            <link>https://medium.com/@karagatan/comparision-of-llm-models-for-coding-fa8e96d4c355?source=rss-bc74cf7fc2b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/fa8e96d4c355</guid>
            <category><![CDATA[id]]></category>
            <category><![CDATA[glue]]></category>
            <dc:creator><![CDATA[Alexander @ Karagatan]]></dc:creator>
            <pubDate>Fri, 06 Mar 2026 12:20:21 GMT</pubDate>
            <atom:updated>2026-03-14T13:10:57.383Z</atom:updated>
            <content:encoded><![CDATA[<p>Today, I would like to compare several LLM models how they build plans to write the code.</p><p>Having some open source projects in internet, I would like to compare the performance of OpenAI Codex and Claude Code on example of some LLM models in cloud and on-premises.</p><p>Having the one open source project that has complex graph of connected logic elements gives me ability to evaluate those solutions in the scope of work of modification of framework for the latest dependencies.</p><p>For this purpose I selected the DI (Dipendency Injection) framework written fully by me from the scratch long time ago, since I was a Java programmer and Spring Framework consultant / architect and participated in the most complex Fortune-500 projects all around the world.</p><p>As a big fan of Spring I found golang is missing nice DI framework at that time and constanty improving my runtime solution that provides out of the box the maximmum functionality similar to spring.</p><p>It was almost 20+ project already written by using this DI framework that now is cornerstone of some production systems and solutions around the world processing massive scale of data on golang with clear component based architecture.</p><p>Here is the project repo: <a href="https://github.com/arpabet/glue">https://github.com/arpabet/glue</a></p><p>The original name of the project I gave was `context`, later I realized a huge confusion with existing package context in golang using everywhere. Therefore I started looking on the name that would fit the best be stopping on `glue` that reflects the nature of DI service for the components.</p><p>First of all I would like to define the scope of the work needed for this framework:</p><ul><li>It is outdated, focusing on golang 1.17+ (needs upgrade)</li><li>It does not support templates, but it is time to add this</li><li>It does not have scopes, and now I think it is time to research how to add them</li><li>It also needs good scaning of beans</li><li>I also want to add a dynamic config for properties.</li></ul><p>Now, let’s select the list of models:</p><ul><li>opus4.6 — this is the best one, but the most expensive</li><li>gpt5.3-codex — nice model from CharGPT</li><li>qwen3.5:122b — local ollama</li><li>gpt-oss:120b — local ollama</li></ul><p>So, for the all those experiments I was using Claude Code except for gpt5.3-codex that was using the Codex.</p><p>After checking out the glue project I asked each model the same prompt:</p><blockquote>I want you to explore the project, make the initial research of it, provide me description and this project for, write list of features and compare with competitors</blockquote><p>Having the best response form opus4.6-high I am publishing it here</p><p><strong>Project Description</strong></p><p><strong>Glue</strong> is a <strong>runtime Dependency Injection (DI) framework for Go</strong>, inspired by the Spring Framework in Java and licensed under <strong>BUSL-1.1</strong> (Business Source License), converting to MPL 2.0 after four years.</p><p>The module path is go.arpabet.com/glue. It has minimal dependencies — only pkg/errors, yaml.v3, and testify (for tests).</p><p>Unlike code-generation-based DI solutions common in Go, Glue uses <strong>reflection</strong> <strong>at</strong> <strong>runtime</strong> to scan struct tags, match interfaces to implementations, and wire dependencies together. The context creation algorithm is O(n×m) — checking every interface against every registered bean.</p><p><strong>Features List</strong></p><pre>  ┌────────────────────┬──────────────────────────────────────────────────────────────────────────────────────────────┐<br>  │      Category      │                                           Feature                                            │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Core DI            │ Runtime dependency injection via inject struct tags on public fields                         │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Bean Types         │ Supports Pointer, Interface, and Function beans                                              │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Collections        │ Inject slices ([]Element) and maps (map[string]Element) of beans                             │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Qualifiers         │ Select specific bean by name: inject:&quot;bean=myService&quot;                                        │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Optional deps      │ inject:&quot;optional&quot; — field stays nil if no candidate found                                    │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Lazy deps          │ inject:&quot;lazy&quot; — defers initialization to break circular dependencies                         │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Lifecycle: Init    │ InitializingBean.PostConstruct() — called after all fields are injected, in dependency order │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Lifecycle: Destroy │ DisposableBean.Destroy() — called on ctx.Close() in reverse init order                       │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Named beans        │ NamedBean.BeanName() — custom name for lookup and map injection keys                         │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Ordered beans      │ OrderedBean.BeanOrder() — controls position in collection injections                         │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Factory beans      │ FactoryBean — programmatic bean creation, singleton or prototype scope                       │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Bean reload        │ Bean.Reload() — destroy + re-init a bean at runtime                                          │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Context hierarchy  │ ctx.Extend(...) — child contexts inherit parent beans, parent is unaware of child            │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Lookup levels      │ Control search depth in context hierarchy (0=fallback, 1=current only, -1=all)               │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Runtime injection  │ ctx.Inject(obj) — inject fields into objects not registered as beans                         │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Properties         │ Built-in key/value property system with .properties, .yaml, .json file support               │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Property injection │ value:&quot;app.port, default=8080&quot; struct tag injects config values with type conversion         │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Property resolvers │ Pluggable PropertyResolver interface with priority ordering                                  │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Resources          │ ResourceSource / Resource — bind and access http.FileSystem assets                           │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Scanner            │ Scanner interface — modular/conditional bean discovery                                       │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Child contexts     │ glue.Child(&quot;name&quot;, beans...) — lazy sub-context creation on demand                           │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Cycle detection    │ Detects circular dependencies at init time and returns clear error messages                  │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Verbose/debug      │ Optional verbose logging of the entire scan/inject/construct pipeline                        │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Thread safety      │ Registry uses sync.RWMutex; runtime Inject uses sync.Map cache                               │<br>  ├────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────┤<br>  │ Graceful close     │ Timeout-based close with panic recovery on destroy                                           │<br>  └────────────────────┴──────────────────────────────────────────────────────────────────────────────────────────────┘</pre><p><strong>Competitor</strong> <strong>Comparison</strong></p><pre>┌────────────────────┬─────────────────────────────┬───────────────────────┬─────────────────────────┬─────────────────┬────────────────────────┐<br>  │      Feature       │            Glue             │      Google Wire      │       Uber Dig/Fx       │ Facebook Inject │       samber/do        │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Approach           │ Runtime reflection          │ Compile-time codegen  │ Runtime reflection      │ Runtime         │ Runtime generics       │<br>  │                    │                             │                       │                         │ reflection      │                        │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Wiring mechanism   │ Struct tags (inject)        │ Provider functions +  │ Constructor functions + │ Struct tags     │ Explicit do.Provide()  │<br>  │                    │                             │ code generation       │  dig.In/dig.Out         │ (inject)        │ / do.MustInvoke()      │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Go version         │ 1.17+                       │ 1.16+                 │ 1.17+                   │ 1.4+            │ 1.18+ (generics)       │<br>  │                    │                             │                       │                         │ (unmaintained)  │                        │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Context hierarchy  │ Yes (Extend + Levels)       │ No                    │ Yes (Fx modules with    │ No              │ Yes (child scopes)     │<br>  │                    │                             │                       │ scopes)                 │                 │                        │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Factory beans      │ Yes (singleton + prototype) │ Via provider funcs    │ Yes (via constructors)  │ No              │ Via lazy providers     │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Lazy injection     │ Yes (inject:&quot;lazy&quot;)         │ N/A (compile-time)    │ No (workarounds exist)  │ No              │ Yes (lazy values)      │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Optional deps      │ Yes (inject:&quot;optional&quot;)     │ No (compile error)    │ Yes (optional:&quot;true&quot;)   │ No              │ Yes                    │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Collection         │ Slices + Maps               │ No (manual grouping)  │ Yes (dig.Group)         │ No              │ No                     │<br>  │ injection          │                             │                       │                         │                 │                        │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Property/config    │ Built-in (value tag,        │ No                    │ No (use Fx config       │ No              │ No                     │<br>  │ injection          │ YAML/JSON/properties)       │                       │ separately)             │                 │                        │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Resource           │ Built-in ResourceSource     │ No                    │ No                      │ No              │ No                     │<br>  │ management         │                             │                       │                         │                 │                        │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Named              │ Yes                         │ Via named providers   │ Yes (dig.Name)          │ No              │ Yes (named services)   │<br>  │ beans/qualifiers   │                             │                       │                         │                 │                        │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Ordered beans      │ Yes (BeanOrder)             │ No                    │ No                      │ No              │ No                     │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Bean reload        │ Yes                         │ No (compile-time)     │ No                      │ No              │ No                     │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Lifecycle hooks    │ PostConstruct + Destroy     │ No                    │ Yes (Fx Lifecycle)      │ No              │ Shutdown hooks         │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Cycle detection    │ Runtime with clear errors   │ Compile-time          │ Runtime with errors     │ Runtime         │ Runtime                │<br>  │                    │                             │                       │                         │ (panics)        │                        │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Type safety        │ Runtime (panics/errors)     │ Compile-time          │ Runtime                 │ Runtime         │ Compile-time           │<br>  │                    │                             │ (strongest)           │                         │                 │ (generics)             │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ Maintenance        │ Active                      │ Active (Google)       │ Active (Uber)           │ Archived        │ Active                 │<br>  ├────────────────────┼─────────────────────────────┼───────────────────────┼─────────────────────────┼─────────────────┼────────────────────────┤<br>  │ License            │ BUSL-1.1 → MPL 2.0          │ Apache 2.0            │ MIT                     │ Apache 2.0      │ MIT                    │<br>  └────────────────────┴─────────────────────────────┴───────────────────────┴─────────────────────────┴─────────────────┴────────────────────────┘</pre><p><strong>Key</strong> <strong>Differentiators</strong></p><p><strong>Strengths</strong> <strong>of</strong> <strong>Glue:</strong></p><p>- Most Spring-like DI experience available in Go — familiar for Java developers migrating to Go</p><p>- Richest built-in feature set in a single package: DI + properties + resources + lifecycle + hierarchy</p><p>- Bean reload at runtime — unique among Go DI frameworks</p><p>- Built-in config injection with multi-format support (properties, YAML, JSON)</p><p>- Ordered collection injection — not available in competitors</p><p><strong>Weaknesses</strong> <strong>/</strong> <strong>Tradeoffs:</strong></p><ul><li><strong>No</strong> <strong>compile-time</strong> <strong>safety</strong> — wiring errors surface at runtime, unlike Google Wire</li><li><strong>Reflection</strong> <strong>overhead</strong> — O(n×m) at context creation; heavier than codegen approaches</li><li><strong>BUSL</strong> <strong>license</strong> — more restrictive than Apache/MIT competitors; may limit adoption in some orgs</li><li><strong>Smaller</strong> <strong>community</strong> — less ecosystem support and documentation compared to Wire or Dig/Fx</li><li><strong>Go</strong> <strong>1.17</strong> <strong>minimum</strong> — doesn’t leverage Go generics for type-safe APIs like samber/do</li></ul><p>It is time to ask LLMs to make a deep research of the project and features and create the enhacement plans, for this purpose I used this prompt equal for all of them</p><blockquote>look on the missing features in glue, think about adding functions injection to “value” properties, for example for lazy or refreshable property <br> initialization. One of the use cases by using PropertyProvider interface some cloud-based solutions could access the secrets in AWS/GCP or <br> whatever, and I would like to think about runtime changing of secrets and automatical reflection of them in glue. This is so called dynamic <br> config. I already have PropertyProvider for the environment variables by keeping the custom logic outside the glue that basically overlook for <br> the property name and if it looks like env name it goes to operation system environments. Besides the propety management it could have <br> improvement with templates and performance. Think about caching injection steps to eliminate O(n*m) compexity. Also think about caching of the <br> properties as well, but keeping in mind dynamic config. Explore the type safety in framework to eliminate interface{}. Think about adding the <br> Profile functionality similar like in Spring Framework. Additionally we can add the scope for the bean injection, so we can inject on runtime <br> request based beans. Thnk about simplification of syntax of the bean injection, support qualifiers or may be it is needed some renamings. Explore <br> the component scanning, by adding scaning by package. May be needed more integration tests and sample applications. Create the modification <br> plan. research and analyse what features could be added, modified in Glue framework, what featureas could be renamed, upgraded, <br> create the plan of the changes. Make this framework better than others.</blockquote><p>After execution I found out that the longest model was actually opus4.6-high, then qwen3.5:122b and then gpt5.3-codex was pretty fast, but I had to make additional prompt for it to create a plan, and gpt-oss:120b was extreamly fast but less accurate.</p><p>Here is the static link to all those plans as they created by LLM for manual evaluation:</p><p><a href="https://github.com/arpabet/glue/tree/v1.2.6/.github/plans">https://github.com/arpabet/glue/tree/v1.2.6/.github/plans</a></p><p>The size of the plans is extreamly different:</p><pre>gpt-oss-plan.md - 5k<br>gpt5.3-codex-plan.md - 7k<br>opus4.6-high-plan.md - 34k<br>qwen3.5-plan.md - 10k</pre><p>For my subjective opinion the best plan is qwen3.5:122b, since it is not so long and have all nessesary features.</p><p>The most reasonable way to do the next steps is to switch to cheaper models. Since I was already using the gpt5.3-codex model in OpenAI, I decided to continue using this one. But for the extermly expensive opus4.6-high I decided to scale down to sonnet-4.6 for the actual implementation.</p><p>But let’s discuss the plans.</p><ul><li>gpt-oss:120b model produced pretty much fast but less accurate plan. I would use this plan if I would not have other options. This plan is pretty basic but even that gives nice overview for the developer. In addition to that I asked this model to create a scanner. It implemented a nice unit test for this and totally incorrect scanner implementation reading source files from filesystem, looks like this model did not realize that golang is compilable language. Skipping.</li><li>gpt5.3-codex:cloud model produced amazing plan that has eveything you need. I liked the naming convensions there in suggestion of keeping FactoryBean and create additional one FactoryBeanOf[T]. I feel that this model thinks like developer and supports backward compatibility. It also suggested to keep PropertyResolver as it is and make additional PropertyProvider. Many things I like from this plan: generic helper methods, WithProfiles context options, glue.IfProfile list of beans, profile-aware properties, qualifier alias, env or secret property expressions, dynamic config approach. In general I feel that this model gives compact and valuable information about the project.</li><li>opus4.6-high:cloud model produced the biggest ever plan. It contains 7 phases that are stong and solid modification plans. By looking on this I feel over-engineering and probably needs additional simplification of those features. But in general I feel like this is the right way to go.</li><li>qwen3.5:122b model produced nice compact plan focusing on list of real problems of the Glue framework. I like how it is structured: performance, type safety, api, missing features. Based on the first recommendation I already made simple name refactorings in IDE. I like how it organized cache lookups without braking any structure of the project, suggested active profiles approach, make a primary bean, added bean scope, typed `FactoryBean[T any]`. I feel like qwen3.5 gives fully comprehensive approach and their recommendations are solid and sufficient.</li></ul><p>The winning LLM model for planing</p><ol><li>opus4.6-high</li><li>gpt5.3-codex</li></ol><p>I feel like the best way is to start with small features from qwen3.5, expand them by gpt5.3-codex proposal, and make comprehensive changes from opus4.6-high.</p><p>I do not think the backward compatibility should be considered at this moment, since the transition to the 1.18+ golang with templates anyway will make the code incompatible, it is literally better to start new version of the framework with all those features in place.</p><p>Finally I used gpt5.4-extended-thinking model to join all those plans together and get final plan.</p><ol><li><strong>Fix performance first.</strong></li><li><strong>Add dynamic config on top of that.</strong></li><li><strong>Introduce typed APIs in a compatibility-safe way.</strong></li><li><strong>Add profiles/scopes next.</strong></li><li><strong>Use code generation for scanning, not brittle runtime scanning.</strong></li><li><strong>Finish with docs, examples, benchmarks, and validation tooling.</strong></li></ol><h3>Single combined modification plan for Glue</h3><h3>Phase 0 — Guardrails and compatibility rules</h3><p>Start with explicit design rules so the rest of the work stays coherent:</p><ul><li>Keep all existing inject and value behavior working for one full compatibility cycle.</li><li>Keep PropertyResolver and current FactoryBean supported.</li><li>Make new behavior opt-in through new interfaces, options, or tag forms.</li><li>Treat Go 1.18+ generics as a staged upgrade: either add typed wrappers behind build tags first, or ship them as part of a v2 branch if you want a clean cut from Go 1.17. This resolves the tension between the plans that want generics immediately and the repo’s current go 1.17 module baseline.</li></ul><h3>Phase 1 — Performance foundation</h3><p>This should be the first implementation milestone, because every later feature benefits from it.</p><p><strong>1.1 Add an injection-plan cache</strong><br> Create plan.go with a global cache keyed by reflect.Type, storing precomputed field/property injection metadata. Move repeated struct-tag parsing and reflection discovery out of hot paths.</p><p><strong>1.2 Build lookup indexes during context creation</strong><br> In context.go and registry.go, replace broad repeated matching with prebuilt indexes:</p><ul><li>concrete type -&gt; beans</li><li>interface type -&gt; candidate beans</li><li>bean name/qualifier -&gt; beans</li><li>ordered collection metadata for slice/map injection</li></ul><p>This turns current startup from repeated global scans toward registration once, lookup many. That directly addresses the current O(n*m) behavior documented by Glue itself.</p><p><strong>1.3 Add positive and negative caches</strong><br> Cache both successful and failed lookups for:</p><ul><li>interface candidate search</li><li>recursive parent-context search</li><li>runtime Inject(...) resolution</li></ul><p><strong>1.4 Add property lookup caching</strong><br> In properties.go, separate:</p><ul><li>static file/map property cache</li><li>dynamic/provider-backed property cache with invalidation hooks</li></ul><p>This phase is the base required by all four plans.</p><p>gpt-oss-plan</p><p>gpt5.3-codex-plan</p><p>opus4.6-high-plan</p><p>qwen3.5-plan</p><h3>Phase 2 — Dynamic configuration and refreshable property injection</h3><p>This is the most valuable functional upgrade after performance.</p><p><strong>2.1 Introduce a richer provider abstraction</strong><br> Keep PropertyResolver working, but add a superset interface, for example PropertyProvider, with:</p><ul><li>Get(key string) (value string, ok bool, err error)</li><li>Version() uint64 or equivalent invalidation signal</li><li>optional watch support for change notifications</li></ul><p>Provide an adapter from existing PropertyResolver so old code continues working.</p><p><strong>2.2 Support function-based </strong><strong>value injection</strong><br> Extend value:&quot;...&quot; injection so fields can be:</p><ul><li>func() T</li><li>func() (T, error)</li><li>optionally func(context.Context) (T, error)</li></ul><p>This lets property-backed values be lazy and refreshable without mutating struct fields in place.</p><p><strong>2.3 Add refresh and invalidation</strong><br> Support:</p><ul><li>explicit invalidation by key</li><li>provider-version invalidation</li><li>optional watch callbacks for secret/config rotation</li><li>a safe opt-in RefreshableBean callback for beans that need to react to changes</li></ul><p><strong>2.4 Add built-in env resolver</strong><br> Ship an EnvPropertyResolver so environment variables are first-class instead of user-only extension code.</p><p><strong>2.5 Add property templates</strong><br> Support ${key} and ${key:default} expressions, with cycle detection and dependency-aware invalidation.</p><p>This is the most practical synthesis of the dynamic-config ideas from all four plans.</p><p>gpt-oss-plan</p><p>gpt5.3-codex-plan</p><p>opus4.6-high-plan</p><h3>Phase 3 — Typed API modernization without breaking existing users</h3><p>The plans agreed on type safety, but they differed on how hard to cut over. The safest unified version is:</p><p><strong>3.1 Add generic helpers first</strong><br> Add typed helpers such as:</p><ul><li>Get[T any](ctx Context) (T, error)</li><li>MustGet[T any](ctx Context) T</li><li>Lookup[T any](ctx Context, level int) ([]T, error)</li></ul><p>These can wrap the existing reflection core instead of replacing it.</p><p><strong>3.2 Add typed factory support</strong><br> Add a typed factory abstraction alongside current FactoryBean, rather than replacing FactoryBean immediately.</p><p><strong>3.3 Keep raw APIs but deprecate them</strong><br> Document the old Inject(interface{}) and other raw entry points as legacy, but keep them functional.</p><p><strong>3.4 Decide Go version policy explicitly</strong><br> Because go.mod is currently go 1.17, you have two viable paths:</p><ul><li>conservative path: typed helpers behind go1.18 build tags while leaving core compatible</li><li>clean path: ship this modernization in glue/v2 and bump minimum Go to 1.18+ or newer</li></ul><p>I would choose the second only if you also want to rename APIs and adopt any everywhere at the same time.</p><h3>Phase 4 — Profiles, conditions, and bean scopes</h3><p>Once property refresh and indexing exist, composition features become much easier.</p><p><strong>4.1 Profiles</strong><br> Add:</p><ul><li>WithProfiles(&quot;dev&quot;, &quot;prod&quot;, ...)</li><li>ProfileBean or equivalent conditional registration contract</li><li>profile-aware property overlays such as base + profile-specific files</li></ul><p><strong>4.2 Conditional beans</strong><br> Add a general ConditionalBean interface for non-profile conditions.</p><p><strong>4.3 Scopes</strong><br> Introduce first-class scopes:</p><ul><li>singleton as default</li><li>prototype</li><li>request</li></ul><p>Prototype should create a fresh instance per retrieval/injection. Request scope should be implemented as a scoped child/runtime context with explicit enter/exit lifecycle.</p><p><strong>4.4 Primary bean selection</strong><br> When multiple beans satisfy an injection, allow a PrimaryBean or explicit primary marker so ambiguous injection can resolve cleanly.</p><p>This phase combines the strongest ideas from the profile/scope proposals while keeping the current singleton/factory model recognizable.</p><p>gpt-oss-plan</p><p>gpt5.3-codex-plan</p><p>opus4.6-high-plan</p><p>qwen3.5-plan</p><h3>Phase 5 — Syntax and ergonomics cleanup</h3><p>Do this after core behavior is stable, not before.</p><p><strong>5.1 Keep old tags, add cleaner aliases</strong><br> Examples:</p><ul><li>allow qualifier= as an alias for bean-name selection</li><li>allow shorthand/default inject forms</li><li>preserve existing optional and lazy</li></ul><p><strong>5.2 Prefer additive ergonomics over hard renames</strong><br> Internal renames are fine anytime. Public renames should be additive first, with deprecation notices.</p><p><strong>5.3 Add a builder only if needed</strong><br> A builder can help for profiles, property sources, and child contexts, but it is lower priority than caching, config refresh, and profiles/scopes.</p><p>This resolves the conflict between “clean up everything first” and “ship meaningful platform features first.” The latter wins.</p><h3>Phase 6 — Scanning, modules, validation, and graph tooling</h3><p>This is where I would deliberately choose one proposal over another.</p><p><strong>Choose code generation, not runtime scanning.</strong><br> Glue’s README explicitly notes that Go does not provide Java-style component scanning, so runtime package scanning is the wrong default. Use a small generator such as gluegen to scan packages and emit registration code, keeping startup deterministic and Go-native.</p><p><strong>6.1 Add </strong><strong>gluegen</strong><br> Generate a registry file from packages containing injectable types.</p><p><strong>6.2 Add graph validation</strong><br> Add a validation mode that checks dependency satisfiability without fully constructing the app.</p><p><strong>6.3 Add graph export</strong><br> Expose DOT graph output for debugging large contexts.</p><p><strong>6.4 Add module/group support</strong><br> Optional Module grouping can help organize large apps, but should compile down to the same registration machinery.</p><p>This phase cleanly merges the “component scanning” ideas while adapting them to Go’s actual constraints.</p><p>gpt5.3-codex-plan</p><p>opus4.6-high-plan</p><h3>Phase 7 — Testing, examples, docs, and performance gates</h3><p>Make this a release criterion, not an afterthought.</p><p>Add:</p><ul><li>benchmarks for startup and lookup at 100 / 1,000 / 5,000 beans</li><li>dynamic config refresh tests</li><li>profile activation tests</li><li>prototype and request-scope lifecycle tests</li><li>typed API compatibility tests</li><li>codegen/scan registry tests</li><li>example apps: web app, dynamic-secret app, multi-profile bootstrap</li></ul><p>Glue already has a decent test surface, so this phase should extend real coverage rather than inventing a new harness.</p><h3>Files to change</h3><p><strong>Primary existing files</strong></p><ul><li>api.go — new interfaces/options, typed/compatibility surface</li><li>bean.go — cached investigation metadata, tag parsing updates</li><li>context.go — indexes, lookup caches, profiles, scopes</li><li>injection.go — new injection execution paths, dynamic property functions</li><li>properties.go — provider chain, cache, invalidation, templates</li><li>registry.go — secondary indexes and profile/scope-aware lookups</li><li>stub.go — compatibility and test stubs</li></ul><p><strong>New files</strong></p><ul><li>plan.go — injection-plan cache</li><li>profile.go</li><li>scope.go</li><li>watcher.go</li><li>env.go</li><li>graph.go</li><li>validate.go</li><li>builder.go only if you keep the builder idea</li><li>cmd/gluegen/... if you adopt generator-based scanning</li></ul><h3>Recommended rollout order</h3><ol><li>Performance/cache layer</li><li>Dynamic property provider + function value injection</li><li>Profiles</li><li>Prototype/request scopes</li><li>Typed helper APIs</li><li>Syntax aliases and primary bean support</li><li>gluegen, validation, graph output</li><li>Examples, migration docs, benchmark gates</li></ol><h3>What I would leave out of the first merged plan</h3><p>These ideas are good, but should not be in the first implementation wave:</p><ul><li>full constructor-injection rewrite</li><li>session/custom scopes beyond request</li><li>decorators/AOP-style wrapping</li><li>broad public API renaming</li><li>aggressive breaking changes to tags</li></ul><p>They are useful, but not as foundational as performance, config refresh, profiles, and scopes.</p><h3>Final recommendation</h3><p>If you want one sentence version: <strong>build Glue vNext around cached injection plans + dynamic properties first, then layer typed APIs, profiles/scopes, and generator-based registration on top, while keeping current tags and interfaces working during the transition.</strong></p><p>If you want, I can turn this into a maintainer-ready RFC with sections for goals, non-goals, API sketches, file-by-file changes, and release sequencing.</p><h3>Summary</h3><p>After implementing all those features, I found out that opus4.6-high and gpt5.4-thinking are working pretty good together and correct each other. Quality of those models are significantly higher than gpt-oss and qwen3.5.</p><p>I added all features to Glue framework and now it is completed.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fa8e96d4c355" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Next Generation of Encrypted Email]]></title>
            <link>https://medium.com/@karagatan/the-next-generation-of-encrypted-email-41b72ebf90cf?source=rss-bc74cf7fc2b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/41b72ebf90cf</guid>
            <category><![CDATA[mail]]></category>
            <category><![CDATA[crypto]]></category>
            <dc:creator><![CDATA[Alexander @ Karagatan]]></dc:creator>
            <pubDate>Thu, 24 Jul 2025 02:55:27 GMT</pubDate>
            <atom:updated>2025-07-24T07:34:01.351Z</atom:updated>
            <content:encoded><![CDATA[<p>Introducing Mailnite’s DNS-based Public Key Discovery</p><h3>Email is Broken — Here’s How We Can Fix It</h3><p>It’s no secret that email, as we use it today, is fundamentally insecure and centralized. Messages are, by default, open to prying eyes, easily forged, and — worst of all — completely controlled by a handful of corporations. Your address, your digital identity, is only yours for as long as a big provider lets you have it.</p><p><strong>Why does this matter?</strong> Because email is the passport to our digital lives: it’s how we recover accounts, sign up for services, and prove who we are. Yet, despite advances everywhere else, email lags behind — still reliant on 1970s architecture that never envisioned today’s privacy and autonomy needs.</p><h3>The Problem: Email’s Privacy and Control Gaps</h3><p>- <strong>Lack of encryption:</strong> Most email is still sent in plaintext. Anyone with access — providers, hackers, state actors — can read your mail.<br> <br>- <strong>Centralized ownership:</strong> Lose your Gmail/Outlook/iCloud account? You lose your digital identity and your history.<br> <br>- <strong>Weak sender authentication:</strong> Phishing is rampant. Anyone can pretend to be anyone.<br> <br>- <strong>Difficult to upgrade:</strong> Attempts at secure email (like PGP) failed to go mainstream because of poor usability and discoverability.</p><h3>A Simple Idea: Publish Public Keys in DNS</h3><p>What if you could easily publish your <strong>public encryption key</strong> as part of your email identity, using the DNS — the same system that already routes your email? What if this information was always under your control, not your provider’s?</p><p>That’s the central idea behind the <a href="https://github.com/mailnite/concept">Mailnite Concept</a>.</p><h3>How Does It Work?</h3><p>- <strong>You generate a public/private keypair</strong> (e.g., using the widely supported <em>secp256k1</em> curve).<br> <br>- <strong>Your public key is published in your domain’s DNS </strong>as a <em>TXT</em> record at a well-known location.<br> <br>- <strong>Anyone who wants to send you a secure message</strong> looks up your key in <em>DNS</em>, encrypts the content, and sends it to you through any standard email system.<br> <br>- <strong>You decrypt the message locally</strong> using your private key, never sharing it with your provider or anyone else.</p><blockquote>No provider lock-in. No opaque login. No gatekeepers. Just math and open standards.</blockquote><h3>The Mailnite Protocol: Key Discovery via DNS</h3><p>The protocol is designed for maximum simplicity and compatibility with existing infrastructure.</p><p><strong>Each user’s key is published at a TXT record named:</strong></p><pre>_mailpubkey.&lt;user&gt;.&lt;domain&gt;</pre><p>The content is a semicolon-delimited string, for example:</p><pre>v=1;pk=BzowchszhjIBqUCDj4wrsysO6BKOJkJsG0-Lbox7u27J;alg=secp256k1;exp=2026–01–01;usr=alice@company.com;pv=mailnite;</pre><h3>Field Breakdown</h3><ul><li><strong>v</strong>: Protocol version.</li><li><strong>pk</strong>: Public key, encoded in Base64 URL-safe format (no padding).</li><li><strong>alg</strong>: The algorithm (e.g., secp256k1).</li><li><strong>exp</strong>: Key expiry date (for rotation and management).</li><li><strong>usr</strong>: External user identifier (such as an email address).</li><li><strong>id</strong>: Optional external ID or mapping value.</li><li><strong>pv</strong>: Provider ID (e.g., mailnite)—this enables new features and commercial support.</li><li><strong>sig</strong>: Optional signature for attestation.</li><li><strong>enc</strong>: Optional preferred encryption algorithm.</li></ul><h3>How to Use It</h3><ol><li><strong>User or admin generates a keypair.</strong></li><li><strong>The public key is published in DNS as a TXT record</strong> for their address.</li><li><strong>Sender queries DNS</strong> for the recipient’s key, encrypts the email, and sends it as usual.</li><li><strong>Receiver’s client/plugin decrypts the message</strong> using the private key.</li></ol><blockquote>Everything else stays the same — no need to change your email server, client, or workflow.</blockquote><h3>Trade-offs and Considerations</h3><p>Let’s make a SWOT analysis.</p><p><strong>Advantages:</strong></p><ul><li><strong>Decentralized</strong>: No single company owns your identity or data.</li><li><strong>Easy discovery</strong>: Public keys are always available at a predictable DNS location.</li><li><strong>Upgrade path</strong>: Compatible with existing email infrastructure and clients (with simple plugins or extensions).</li><li><strong>Extensible</strong>: Fields like pv allow for new commercial or open features.</li></ul><p><strong>Trade-offs:</strong></p><ul><li><strong>Doesn’t stop spam</strong>: Spammers can still send to your address. (But encrypted mail is less useful to them.)</li><li><strong>Key management</strong>: Users need to keep their private key safe (but this is true of all real cryptography).</li><li><strong>DNS privacy</strong>: DNS queries for your key can be observed by network operators. (Solutions: DNS-over-HTTPS, or storing only a hash/mapping.)</li></ul><h3>See the Full Protocol Documentation</h3><p>The <a href="https://github.com/mailnite/concept">Mailnite Concept Repository</a> includes a living specification, implementation notes, and example scripts. Here’s an <a href="https://github.com/mailnite/concept#example">example record</a>:</p><pre>_mailpubkey.alice.example.com. IN TXT &quot;v=1;pk=BzowchszhjIBqUCDj4wrsysO6BKOJkJsG0-Lbox7u27J;alg=secp256k1;exp=2026-01-01;usr=alice@company.com;pv=mailnite;&quot;</pre><p><strong>Want to try it?</strong></p><ul><li>Follow the <a href="https://github.com/mailnite/concept#quickstart">step-by-step guide</a>.</li><li>Generate your keypair and publish your public key.</li><li>Use or build a compatible client or plugin (coming soon).</li></ul><h3>Join the Conversation</h3><p><a href="https://mailnite.com/">Mailnite</a> is an <strong>open protocol</strong> — your feedback, ideas, and pull requests are welcome!<br>We believe privacy and user control should be the default, not an add-on.</p><p>Mailnite PoC code is available <a href="https://github.com/mailnite/mailnite-poc">here</a>.</p><p><strong>Read more, contribute, and follow the progress at </strong><a href="https://github.com/mailnite/concept"><strong>github.com/mailnite/concept</strong></a><strong>.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41b72ebf90cf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploying K8S application in GKE to GCP Endpoint. Part 4.]]></title>
            <link>https://medium.com/@karagatan/deploying-k8s-application-in-gke-to-gcp-endpoint-part-4-d3a620cedafe?source=rss-bc74cf7fc2b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3a620cedafe</guid>
            <category><![CDATA[gke]]></category>
            <category><![CDATA[terraform]]></category>
            <dc:creator><![CDATA[Alexander @ Karagatan]]></dc:creator>
            <pubDate>Tue, 22 Jul 2025 06:02:09 GMT</pubDate>
            <atom:updated>2025-07-22T06:02:09.410Z</atom:updated>
            <content:encoded><![CDATA[<p>Building the CI/CD pipeline based on Terraform to deploy application in GKE cluster.</p><p>In previous <a href="https://medium.com/@karagatan/fastapi-simple-rest-application-in-gke-with-terraform-ci-cd-part-3-2365299e8cc6">Part 3</a> we created a simple web application that is using FAST API framework in python to expose REST API to public.</p><p>Now it is time to deploy this application to the GKE service, build CI/CD pipeline as expose it as a public GCP Endpoint.</p><p>Let’s start by adding the tool file write_openapi.pythat would generate openapi.yaml</p><pre>import json<br>import yaml<br>from copy import deepcopy<br>from fastapi.openapi.utils import get_openapi<br>from config.config import project_id, secret_name<br><br>def convert_v3_to_v2(openapi_v3: dict) -&gt; dict:<br>    &quot;&quot;&quot;<br>    Minimal conversion from OpenAPI v3 to v2 spec.<br>    Supports:<br>      - info<br>      - host (added separately)<br>      - basePath (&quot;/&quot;)<br>      - schemes ([&quot;https&quot;, &quot;http&quot;])<br>      - paths with GET/POST and simple response schemas (type, properties)<br>    &quot;&quot;&quot;<br><br>    openapi_v2 = {}<br><br>    # Basic top-level fields<br>    openapi_v2[&quot;swagger&quot;] = &quot;2.0&quot;<br>    openapi_v2[&quot;info&quot;] = openapi_v3.get(&quot;info&quot;, {})<br><br>    # Convert paths<br>    paths_v3 = openapi_v3.get(&quot;paths&quot;, {})<br>    paths_v2 = {}<br><br>    for path, methods in paths_v3.items():<br>        paths_v2[path] = {}<br>        for method, details in methods.items():<br>            op = {}<br>            op[&quot;summary&quot;] = details.get(&quot;summary&quot;, &quot;&quot;)<br>            op[&quot;operationId&quot;] = details.get(&quot;operationId&quot;, None)<br>            op[&quot;produces&quot;] = [&quot;application/json&quot;]<br><br>            # Parameters (query/path/body)<br>            parameters = []<br>            for param in details.get(&quot;parameters&quot;, []):<br>                param_v2 = deepcopy(param)<br>                # OpenAPI v3 uses &quot;schema&quot; for body/query params, v2 uses &quot;type&quot;<br>                if &quot;schema&quot; in param_v2:<br>                    schema = param_v2.pop(&quot;schema&quot;)<br>                    if &quot;type&quot; in schema:<br>                        param_v2[&quot;type&quot;] = schema[&quot;type&quot;]<br>                parameters.append(param_v2)<br>            op[&quot;parameters&quot;] = parameters<br><br>            # Responses: convert content-&gt;schema to v2 schema<br>            responses = {}<br>            for code, resp in details.get(&quot;responses&quot;, {}).items():<br>                resp_v2 = {}<br>                description = resp.get(&quot;description&quot;, &quot;&quot;)<br>                resp_v2[&quot;description&quot;] = description<br><br>                # Convert OpenAPI v3 response content to v2 schema<br>                content = resp.get(&quot;content&quot;, {})<br>                if &quot;application/json&quot; in content:<br>                    schema = content[&quot;application/json&quot;].get(&quot;schema&quot;, None)<br>                    if schema:<br>                        resp_v2[&quot;schema&quot;] = schema<br><br>                responses[code] = resp_v2<br>            op[&quot;responses&quot;] = responses<br><br>            paths_v2[path][method] = op<br><br>    openapi_v2[&quot;paths&quot;] = paths_v2<br><br>    # Remove servers (not in v2)<br>    if &quot;servers&quot; in openapi_v3:<br>        del openapi_v3[&quot;servers&quot;]<br><br>    return openapi_v2<br><br><br><br>def generate_openapi_yaml(host, target, output_file):<br>    &quot;&quot;&quot;Generate OpenAPI schema in YAML format for GCP Cloud Endpoints.&quot;&quot;&quot;<br>    from app import app<br><br>    schema = get_openapi(<br>        title=f&quot;{service_name} API HTTP Service&quot;,<br>        version=&quot;1.0.0&quot;,<br>        description=f&quot;{service_name} API HTTP Service for GKE service&quot;,<br>        routes=app.routes,<br>    )<br><br>    # Manually include /docs and /openapi.json<br>    schema.setdefault(&quot;paths&quot;, {})<br>    schema[&quot;paths&quot;][&quot;/docs&quot;] = {<br>        &quot;get&quot;: {<br>            &quot;summary&quot;: &quot;Swagger UI&quot;,<br>            &quot;description&quot;: &quot;Swagger UI for this API&quot;,<br>            &quot;operationId&quot;: &quot;getDocs&quot;,<br>            &quot;responses&quot;: {&quot;200&quot;: {&quot;description&quot;: &quot;Swagger UI&quot;}},<br>        }<br>    }<br>    schema[&quot;paths&quot;][&quot;/openapi.json&quot;] = {<br>        &quot;get&quot;: {<br>            &quot;summary&quot;: &quot;OpenAPI schema&quot;,<br>            &quot;description&quot;: &quot;OpenAPI JSON for this API&quot;,<br>            &quot;operationId&quot;: &quot;getOpenapiSchema&quot;,<br>            &quot;responses&quot;: {&quot;200&quot;: {&quot;description&quot;: &quot;OpenAPI schema&quot;}},<br>        }<br>    }<br><br>    openapi_schema = convert_v3_to_v2(schema)<br><br>    openapi_schema[&quot;swagger&quot;] = &quot;2.0&quot;<br>    openapi_schema[&quot;host&quot;] = host<br>    openapi_schema[&quot;basePath&quot;] = &quot;/&quot;<br>    openapi_schema[&quot;schemes&quot;] = [&quot;https&quot;, &quot;http&quot;]<br>    openapi_schema[&quot;x-google-endpoints&quot;] = [<br>        {<br>            &quot;name&quot;: host,<br>            &quot;target&quot;: target,<br>        }<br>    ]<br><br>    with open(output_file, &quot;w&quot;) as f:<br>        yaml.dump(openapi_schema, f, sort_keys=False)<br><br>    print(&quot;✅ OpenAPI schema written to openapi.yaml&quot;)<br><br><br><br># GCP Cloud Endpoints configuration<br>GCP_SERVICE_HOST = f&quot;{service_name}.endpoints.{project_id}.cloud.goog&quot;<br>OUTPUT_FILE = &quot;openapi.yaml&quot;<br><br><br>def main():<br>    parser = argparse.ArgumentParser(description=&quot;FastAPI app or OpenAPI generator for GCP&quot;)<br>    parser.add_argument(&quot;--host&quot;, default=GCP_SERVICE_HOST, help=&quot;Host to run FastAPI server on&quot;)<br>    parser.add_argument(&quot;--target&quot;, help=&quot;External IP of the GCP Endpoint&quot;)<br>    parser.add_argument(&quot;--output&quot;, default=OUTPUT_FILE, help=&quot;Output file of the schema&quot;)<br><br>    args = parser.parse_args()<br><br>    generate_openapi_yaml(host=args.host, target=args.target, output_file=args.output)<br>    sys.exit(0)<br><br>if __name__ == &quot;__main__&quot;:<br>    main()<br></pre><p>This script will ask FAST API to provide all known routes, and additionally adds specific for FAST API routes like /docs/ and /openapi.json to the schema, adds external ip address that we got in the <a href="https://medium.com/@karagatan/ingress-controller-in-k8s-cluster-on-gcp-by-terraform-part-2-202fad81dc2d">Part 2</a> and writes to the file openapi.yaml</p><pre>swagger: &#39;2.0&#39;<br>info:<br>  title: example-api-service API HTTP Service<br>  description: example-api-service API HTTP Service for GKE service<br>  version: 1.0.0<br>host: example-api-service.endpoints.devops-example-project.cloud.goog<br>basePath: /<br>schemes:<br>- https<br>- http<br>x-google-endpoints:<br>- name: example-api-service.endpoints.devops-example-project.cloud.goog<br>  target: &lt;your external static ip&gt;<br>paths:<br>  /:<br>    get:<br>      summary: Read Root<br>      operationId: read_root__get<br>      produces:<br>      - application/json<br>      parameters: []<br>      responses:<br>        &#39;200&#39;:<br>          description: Successful Response<br>  /v1/status:<br>    get:<br>      summary: Status<br>      operationId: status_v1_status_get<br>      produces:<br>      - application/json<br>      parameters: []<br>      responses:<br>        &#39;200&#39;:<br>          description: Successful Response<br>  /docs:<br>    get:<br>      summary: Swagger UI<br>      operationId: getDocs<br>      produces:<br>      - application/json<br>      parameters: []<br>      responses:<br>        &#39;200&#39;:<br>          description: Swagger UI<br>  /openapi.json:<br>    get:<br>      summary: OpenAPI schema<br>      operationId: getOpenapiSchema<br>      produces:<br>      - application/json<br>      parameters: []<br>      responses:<br>        &#39;200&#39;:<br>          description: OpenAPI schema</pre><p>Now you have a tool script in the project that can generate schema after the API changes automatically.</p><p>Let’s register it in GCP Endpoints, so it would create an endpoint for the first time call or update it for sequential calls.</p><pre>gcloud endpoints services deploy openapi.yaml --project=devops-example-project </pre><p>In GCP Web interface in ‘Endpoints’ section you can see that endpoint was added with declared interface.</p><p>Now there is the time to create additional GCP Service Account that we are going to use to run this first application in GKE.</p><pre>PROJECT=devops-example-project<br>GKE_SERVICES_SA=devops-gke-services<br><br>gcloud iam service-accounts create $GKE_SERVICES_SA \<br>  --project=$PROJECT \<br>  --description=&quot;API Services GKE Service Account&quot; \<br>  --display-name=&quot;API Services GKE SA&quot;<br><br>gcloud projects add-iam-policy-binding $PROJECT \<br>  --member=&quot;serviceAccount:$GKE_SERVICES_SA@$PROJECT.iam.gserviceaccount.com&quot; \<br>  --role=&quot;roles/servicemanagement.serviceController&quot;<br><br>gcloud secrets add-iam-policy-binding $PROJECT \<br>  --member=&quot;serviceAccount:$GKE_SERVICES_SA@$PROJECT.iam.gserviceaccount.com&quot; \<br>  --role=&quot;roles/secretmanager.secretAccessor&quot;</pre><p>Let’s open GCP Web Interface, go to the <em>IAM&amp;Admin</em> and find thie Service Account <strong>devops-gke-services</strong>, copy the <strong>JSON</strong> content and convert it to <strong>Base</strong>⁶⁴. We would use this service account as <em>GCP_SERVICES_SA_KEY</em> in our terraform and workflow scripts. Together with runtime <em>GCP_SERVICES_SA_KEY </em>do not forget to add <em>GCP_DEPLOYMENTS_SA_KEY</em> that we created in <a href="https://medium.com/@karagatan/ingress-controller-in-k8s-cluster-on-gcp-by-terraform-part-2-202fad81dc2d">Part 2</a>.</p><p>Let’s add it to the repository in GitHub as Base64 value in 1 or in 2:</p><ol><li>GitHub -&gt; Source Code Repo -&gt; Settings -&gt; Secrets and variables -&gt; Actions -&gt; Repository secrets.</li><li>GitHub -&gt; Organization &gt; Settings -&gt; Secret and variables -&gt; Actions -&gt; Organization secrets.</li></ol><p>Now, we have compled registration ot GCP endpoint, GCP Service Account and GitHub reference on Service Account.</p><h3>Terraform</h3><p>For the next step we would need ready terraform scripts.</p><p>All scripts are located in terraform folder in source code repository.</p><p>Let’s create .terraform-version file.</p><pre>1.12.1</pre><p>Now we can define variables for the service.</p><pre>variable &quot;project_id&quot; {<br>  type = string<br>}<br><br>variable &quot;region&quot; {<br>  type = string<br>}<br><br>variable &quot;region_with_zone&quot; {<br>  type = string<br>}<br><br>variable &quot;cluster_name&quot; {<br>  type = string<br>}<br><br>variable &quot;image_name&quot; {<br>  type = string<br>}<br><br>variable &quot;image_tag&quot; {<br>  type = string<br>  default = &quot;latest&quot;<br>}<br><br>variable &quot;repo_name&quot; {<br>  type = string<br>}<br><br>variable &quot;service_account_key&quot; {<br>  type      = string<br>  sensitive = true<br>}</pre><p>With predefined parameters in terraform.tfvars file</p><pre><br>image_name = &quot;example-api-service&quot;<br><br>project_id = &quot;devops-example-project&quot;<br>region = &quot;us-central1&quot;<br>region_with_zone = &quot;us-central1-a&quot;<br>repo_name = &quot;devops-docker-repo&quot;<br><br># do not put here service_account_key</pre><p>From those parameters we are going to change only image_name if we create another project.</p><p>Let’s create backend.tf file</p><pre>terraform {<br>  backend &quot;gcs&quot; {<br>    bucket  = &quot;devops-terraform-state&quot; # You must create this bucket first<br>    prefix  = &quot;devops-cluster-dev/example-api-service&quot;<br>  }<br>}</pre><p>Now we are ready to add main.tf file that created major resources</p><pre>provider &quot;google&quot; {<br>  project = var.project_id<br>  region  = var.region_with_zone<br>}<br><br>data &quot;google_client_config&quot; &quot;default&quot; {}<br><br>data &quot;google_container_cluster&quot; &quot;gke&quot; {<br>  name     = var.cluster_name<br>  location = var.region_with_zone<br>}<br><br>provider &quot;kubernetes&quot; {<br>  host                   = &quot;https://${data.google_container_cluster.gke.endpoint}&quot;<br>  cluster_ca_certificate = base64decode(data.google_container_cluster.gke.master_auth[0].cluster_ca_certificate)<br>  token                  = data.google_client_config.default.access_token<br>}<br><br>resource &quot;kubernetes_namespace&quot; &quot;current&quot; {<br>  metadata {<br>    name = &quot;${var.image_name}&quot;<br>  }<br><br>  lifecycle {<br>    ignore_changes = [metadata]<br>  }<br>}</pre><p>Here we are not creating gke cluster, but using exiting one defined in <a href="https://medium.com/@karagatan/operating-k8s-cluster-on-gcp-by-terraform-part-1-8356ac33bb86">Part 1</a>.</p><p>The principal idea is to create a separate namespace for each deployment of service or application, that what we are doing here in kubernetes_namespace.current . In case if something significantly went wrong in kubectl we can delete the namespace of the application and re-deploy it again, especially if it is a stateless deployment.</p><p>Now it is the time to define our primary service.tf file.</p><pre>resource &quot;kubernetes_deployment&quot; &quot;api_deployment&quot; {<br>  metadata {<br>    name      = var.image_name<br>    namespace = kubernetes_namespace.current.metadata[0].name<br>    labels = {<br>      app = var.image_name<br>    }<br>  }<br><br>  spec {<br>    replicas = 1<br><br>    selector {<br>      match_labels = {<br>        app = var.image_name<br>      }<br>    }<br><br>    template {<br>      metadata {<br>        labels = {<br>          app = var.image_name<br>        }<br>      }<br><br>      spec {<br>        container {<br>          name  = &quot;${var.image_name}&quot;<br>          image = &quot;${var.region}-docker.pkg.dev/${var.project_id}/${var.repo_name}/${var.image_name}:${var.image_tag}&quot;<br><br>          port {<br>            container_port = 8000<br>          }<br><br>          resources {<br>            limits = {<br>              cpu    = &quot;250m&quot;<br>              memory = &quot;512Mi&quot;<br>            }<br>            requests = {<br>              cpu    = &quot;100m&quot;<br>              memory = &quot;256Mi&quot;<br>            }<br>          }<br><br>          readiness_probe {<br>            http_get {<br>              path = &quot;/&quot;<br>              port = 8000<br>            }<br>            initial_delay_seconds = 2<br>            period_seconds        = 5<br>          }<br><br>          liveness_probe {<br>            http_get {<br>              path = &quot;/&quot;<br>              port = 8000<br>            }<br>            initial_delay_seconds = 5<br>            period_seconds        = 10<br>          }<br><br>          env {<br>            name  = &quot;PORT&quot;<br>            value = &quot;8000&quot;<br>          }<br>        }<br>      }<br>    }<br>  }<br><br>  depends_on = [kubernetes_namespace.current]<br>}<br><br>resource &quot;kubernetes_service&quot; &quot;api_service&quot; {<br>  metadata {<br>    name      = var.image_name<br>    namespace = kubernetes_namespace.current.metadata[0].name<br>  }<br><br>  spec {<br>    selector = {<br>      app = var.image_name<br>    }<br><br>    port {<br>      port        = 80<br>      target_port = 8000<br>      protocol    = &quot;TCP&quot;<br>    }<br><br>    type = &quot;ClusterIP&quot;<br>  }<br><br>  depends_on = [kubernetes_namespace.current]<br>}</pre><p>The FAST API and Dockerfile are exporting 8000 port, not the 80 port, therefore in out api_service we need to redirect those ports. This file also contains the api_deployment that defines the actual service running on 8000 port and using image that we built earlier. Resources section could be used to increase/decrease resources of the particular pod. The two sections readiness_probe and liveness_probe guarantee that Terraform will go to the next step only after ensuring that root path is working, that is required by GCP/GKE, that’s why we have root endpoint in our service.</p><p>The next file we need is proxy.tf . This file is responsible to work with GCP Endpoint and track all calls of the methods that we automatically declared in openapi.xml . The proxy name is esp-v2. First we forward all traffic to proxy and proxy itself forward traffic to the service.</p><pre># usually nothing to change here by @Alex<br><br>resource &quot;kubernetes_deployment&quot; &quot;proxy_deployment&quot; {<br>  metadata {<br>    name      = &quot;${var.image_name}-proxy&quot;<br>    namespace = kubernetes_namespace.current.metadata[0].name<br>    labels = {<br>      app = &quot;${var.image_name}-proxy&quot;<br>    }<br>  }<br><br>  spec {<br>    replicas = 1<br><br>    selector {<br>      match_labels = {<br>        app = &quot;${var.image_name}-proxy&quot;<br>      }<br>    }<br><br>    template {<br>      metadata {<br>        labels = {<br>          app = &quot;${var.image_name}-proxy&quot;<br>        }<br>      }<br><br>      spec {<br>        container {<br>          name  = &quot;${var.image_name}-proxy&quot;<br>          image = &quot;gcr.io/endpoints-release/endpoints-runtime:2&quot;<br><br>          args = [<br>            &quot;--listener_port=8080&quot;,<br>            &quot;--backend=${kubernetes_service.api_service.metadata[0].name}:80&quot;,<br>            &quot;--service=${var.image_name}.endpoints.${var.project_id}.cloud.goog&quot;,<br>            &quot;--rollout_strategy=managed&quot;,<br>          ]<br><br>          port {<br>            container_port = 8080<br>          }<br><br>          resources {<br>            limits = {<br>              cpu    = &quot;500m&quot;<br>              memory = &quot;512Mi&quot;<br>            }<br>            requests = {<br>              cpu    = &quot;100m&quot;<br>              memory = &quot;128Mi&quot;<br>            }<br>          }<br><br>          readiness_probe {<br>            http_get {<br>              path = &quot;/&quot;<br>              port = 8080<br>            }<br>            initial_delay_seconds = 2<br>            period_seconds        = 5<br>          }<br><br>          liveness_probe {<br>            http_get {<br>              path = &quot;/&quot;<br>              port = 8080<br>            }<br>            initial_delay_seconds = 5<br>            period_seconds        = 10<br>          }<br><br>        }<br>      }<br>    }<br>  }<br><br>  depends_on = [kubernetes_namespace.current, kubernetes_service.api_service]<br>}<br><br>resource &quot;kubernetes_service&quot; &quot;proxy_service&quot; {<br>  metadata {<br>    name      = &quot;${var.image_name}-proxy&quot;<br>    namespace = kubernetes_namespace.current.metadata[0].name<br>  }<br><br>  spec {<br>    selector = {<br>      app = &quot;${var.image_name}-proxy&quot;<br>    }<br><br>    port {<br>      port        = 80<br>      target_port = 8080<br>      protocol    = &quot;TCP&quot;<br>    }<br><br>    type = &quot;ClusterIP&quot;<br>  }<br><br>  depends_on = [kubernetes_namespace.current]<br>}</pre><p>It is important to provide the correct domain name of the GCP Endpoint to this proxy and correct port to the service. I kept it simple and using in all services 80 port.</p><p>In order to serve the traffic we need to create ingress resource and define what host we are going to listen.</p><p>Let’s create the ingress.tf file.</p><pre>resource &quot;kubernetes_manifest&quot; &quot;ingress&quot; {<br>  manifest = {<br>    apiVersion = &quot;networking.k8s.io/v1&quot;<br>    kind       = &quot;Ingress&quot;<br>    metadata = {<br>      name      = &quot;${var.image_name}-ingress&quot;<br>      namespace = kubernetes_namespace.current.metadata[0].name<br>      annotations = {<br>        &quot;cert-manager.io/cluster-issuer&quot;                = &quot;letsencrypt-prod&quot;<br>        &quot;cert-manager.io/issue-temporary-certificate&quot;   = &quot;true&quot;<br>        &quot;cert-manager.io/acme-challenge-type&quot;            = &quot;http01&quot;<br>      }<br>    }<br>    spec = {<br>      ingressClassName = &quot;nginx&quot;<br>      rules = [<br>        {<br>          host = &quot;${var.image_name}.endpoints.${var.project_id}.cloud.goog&quot;<br>          http = {<br>            paths = [<br>              {<br>                path     = &quot;/&quot;<br>                pathType = &quot;Prefix&quot;<br>                backend = {<br>                  service = {<br>                    name = kubernetes_service.proxy_service.metadata[0].name<br>                    port = {<br>                      number = 80<br>                    }<br>                  }<br>                }<br>              }<br>            ]<br>          }<br>        },<br>      ]<br>      tls = [<br>        {<br>          hosts      = [&quot;${var.image_name}.endpoints.${var.project_id}.cloud.goog&quot;]<br>          secretName = &quot;${var.image_name}-tls&quot;<br>        }<br>      ]<br>    }<br>  }<br><br>  field_manager {<br>    name            = &quot;terraform&quot;<br>    force_conflicts = true<br>  }<br><br>  depends_on = [kubernetes_namespace.current, kubernetes_service.proxy_service]<br>}</pre><p>In this file we forward all traffic to the proxy_service that we defined earlier. Also we are declaring the serverName for the TLS certificate that would be generated automatically since we have special annotations for that.</p><p>And finally we need outputs.tf file to display terraform variables.</p><pre><br>output &quot;namespace&quot; {<br>  description = &quot;Namespace where the application is deployed&quot;<br>  value       = kubernetes_namespace.current.metadata[0].name<br>}<br><br>output &quot;image_used&quot; {<br>  description = &quot;Docker image deployed&quot;<br>  value       = &quot;${var.region}-docker.pkg.dev/${var.project_id}/${var.repo_name}/${var.image_name}:${var.image_tag}&quot;<br>}</pre><p>We want to know that <strong>namespace</strong> was used and what <strong>image</strong> was deployed.</p><h3>CI/CD</h3><p>Now we have fully developed application and terraform scripts, created GCP endpoint and service account.</p><p>Let’s create the file .github/workflows/deploy-gke.yaml</p><pre>name: Build and Push to GCP<br><br>on:<br>  push:<br>    branches:<br>      - main <br><br>env:<br>  IMAGE_NAME: example-api-service<br>  PROJECT_ID: devops-example-project<br>  REGION: us-central1<br>  REGION_WITH_ZONE: us-central1-a<br>  REPOSITORY: docker.pkg.dev<br>  REPO_NAME: devops-docker-repo<br>  CLUSTER_NAME: devops-cluster<br><br>jobs:<br>  build-and-push:<br>    runs-on: ubuntu-latest<br><br>    outputs:<br>      image_tag: ${{ github.sha }}<br><br>    steps:<br>      - name: Checkout source code<br>        uses: actions/checkout@v3<br><br>      - name: Set up Docker<br>        uses: docker/setup-buildx-action@v3<br><br>      - name: Authenticate to Google Cloud<br>        uses: google-github-actions/auth@v2<br>        with:<br>          credentials_json: &#39;${{ secrets.GCP_DEPLOYMENTS_SA_KEY }}&#39;<br><br>      - name: Configure Docker for Artifact Registry<br>        run: |<br>          gcloud auth configure-docker ${{ env.REGION }}-${{ env.REPOSITORY }} --quiet<br><br>      - name: Build and Push Docker image<br>        run: |<br>          IMAGE_SHA=${{ env.REGION }}-${{ env.REPOSITORY }}/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/${{ env.IMAGE_NAME }}:${{ github.sha }}<br>          IMAGE_LATEST=${{ env.REGION }}-${{ env.REPOSITORY }}/${{ env.PROJECT_ID }}/${{ env.REPO_NAME }}/${{ env.IMAGE_NAME }}:latest<br><br>          docker build -t $IMAGE_SHA -t $IMAGE_LATEST -f Dockerfile.dev .<br>          docker push $IMAGE_SHA<br>          docker push $IMAGE_LATEST<br><br>          echo &quot;Pushed images:&quot;<br>          echo &quot;  - $IMAGE_SHA&quot;<br>          echo &quot;  - $IMAGE_LATEST&quot;<br><br>  terraform-deploy:<br>    needs: build-and-push<br>    runs-on: ubuntu-latest<br><br>    defaults:<br>      run:<br>        working-directory: terraform<br><br>    steps:<br>      - name: Checkout source code<br>        uses: actions/checkout@v3<br><br>      - name: Set up Terraform<br>        uses: hashicorp/setup-terraform@v2<br>        with:<br>          terraform_version: 1.12.1<br><br>      - name: Authenticate to Google Cloud<br>        uses: google-github-actions/auth@v2<br>        with:<br>          credentials_json: &#39;${{ secrets.GCP_DEPLOYMENTS_SA_KEY }}&#39;<br><br>      - name: Get GKE Credentials<br>        run: |<br>          gcloud container clusters get-credentials ${{ env.CLUSTER_NAME }} \<br>            --region ${{ env.REGION_WITH_ZONE }} \<br>            --project ${{ env.PROJECT_ID }}<br><br>      - name: Terraform Init<br>        run: terraform init<br><br>      - name: Terraform Plan<br>        run: terraform plan -var=&quot;image_tag=${{ needs.build-and-push.outputs.image_tag }}&quot;<br>        working-directory: terraform<br>        env:<br>          TF_VAR_service_account_key: ${{ secrets.GCP_SERVICES_SA_KEY }}<br><br>      - name: Terraform Apply<br>        run: terraform apply -auto-approve -var=&quot;image_tag=${{ needs.build-and-push.outputs.image_tag }}&quot;<br>        working-directory: terraform<br>        env:<br>          TF_VAR_service_account_key: ${{ secrets.GCP_SERVICES_SA_KEY }}</pre><p>The CI/CD pipeline successfully created and after each commit you will have new service deployed in GKE cluster by terraform.</p><h3>Summary</h3><p>In this Part we finished the set of articles that are using Terraform for GKE cluster creation and automation with GitHub workflows.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3a620cedafe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[FastAPI simple REST application in GKE with Terraform CI/CD. Part 3.]]></title>
            <link>https://medium.com/@karagatan/fastapi-simple-rest-application-in-gke-with-terraform-ci-cd-part-3-2365299e8cc6?source=rss-bc74cf7fc2b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/2365299e8cc6</guid>
            <category><![CDATA[fastapi]]></category>
            <category><![CDATA[gke]]></category>
            <dc:creator><![CDATA[Alexander @ Karagatan]]></dc:creator>
            <pubDate>Mon, 21 Jul 2025 00:42:06 GMT</pubDate>
            <atom:updated>2025-07-21T00:42:06.084Z</atom:updated>
            <content:encoded><![CDATA[<p>Building simple REST API application of Python using our Terraform GKE infra.</p><p>In previous <a href="https://medium.com/@karagatan/ingress-controller-in-k8s-cluster-on-gcp-by-terraform-part-2-202fad81dc2d">Part 2</a> we created the Terraform infrastructure with CI/CD pipeline on GitHub Actions what includes automatic certificate management and HTTPS ingress controller.</p><p>Let’s use this infrastructure to deploy the first application that would provide API capabilities of our services.</p><p>First of all we need to register the domain name or use existing GCP endpoints. We will go forward with second option since it is only the API.</p><h3>Recap</h3><p>We have those environments:</p><pre>PROJECT=devops-example-project<br>REGION=us-central1<br>REGION_WITH_ZONE=us-central1-a<br>CLUSTER_NAME=devops-cluster<br>EXTERNAL_IP=&lt;your external ip address&gt;</pre><p>Here we are adding one more, this service name</p><pre>SERVICE_NAME=example-api-service</pre><p>The next step would be to authenticate in GCP to have a working environment.</p><pre>gcloud auth login<br>gcloud config set project devops-example-project</pre><p>Now we are ready to implement the first API application.</p><h3>API Service</h3><p>For the service we should create a source code repository in GitHub with this file structure:</p><pre>config<br>    __init__.py<br>    config.py<br>routers<br>    __init__.py<br>    service_api.py<br>schemas<br>    __init__.py<br>    schemas.py<br>utils<br>    __init__.py<br>    load_secrets.py<br>    central_log.py<br>terraform<br>    .terraform-version<br>    backend.tf<br>    ingress.tf<br>    main.tf<br>    outputs.tf<br>    proxy.tf<br>    service.tf<br>    terraform.tfvars<br>    variables.tf<br>.env<br>.gitignore<br>app.py<br>main.py<br>Dockerfile<br>Makefile<br>requirements.txt</pre><p>The config module is needed to keep some specifics for the project, for example the secret name and project id that are going to be used to get secrets from GCP Secret Manager.</p><p>The config.py content could look like this</p><pre><br>import os<br><br>project_id = &quot;devops-example-project&quot;<br>service_name = &quot;example-api-service&quot;<br>version = &quot;1.0.0&quot;<br>env_name = os.getenv(&quot;ENV_NAME&quot;, &quot;local&quot;)</pre><p>In the real application you could have complex logic to initialize config and take those parameters from environments.</p><p>Now we can implement the first utility module using those configs: utils/load_secrets.py</p><pre>import os<br>from google.cloud import secretmanager<br>from google.oauth2 import service_account<br>import json<br>import base64<br>from config.config import project_id, service_name<br><br>def load_secrets():<br><br>    credentials = None<br><br>    # Check for base64 credentials<br>    b64_creds = os.getenv(&quot;SERVICE_ACCOUNT_KEY&quot;)<br>    if b64_creds:<br>        creds_json = json.loads(base64.b64decode(b64_creds).decode(&quot;utf-8&quot;))<br>        credentials = service_account.Credentials.from_service_account_info(creds_json)<br><br>    # Initialize Secret Manager client<br>    client = secretmanager.SecretManagerServiceClient(credentials=credentials)<br>    secret_name = f&quot;projects/{project_id}/secrets/{service_name}/versions/latest&quot;<br><br>    try:<br>        response = client.access_secret_version(name=secret_name)<br>        secret_payload = response.payload.data.decode(&quot;UTF-8&quot;)<br>        secrets = json.loads(secret_payload)<br><br>        for key, value in secrets.items():<br>            os.environ[key] = str(value)<br><br>    except Exception as e:<br>        print(f&quot;Error loading secrets: {e}&quot;)<br>        raise</pre><p>Why this utility method is not in the config.py? The reason for this is that we want to initialize the secrets only on startup of the application. You potentially could have conditional logic to load them from GCP Secrets or from .env file for development. Additionally, this util class is using <em>SERVICE_ACCOUNT_KEY</em> environment variable, that expects to have the content of <em>GOOGLE_APPLICATION_CREDENTIALS</em> file encoded in <em>Base64</em>.</p><p>The good practice is to have central logging system for the application from the earlier beginning. This helps to keep this logic in one place and log it outside of the application. Let’s create the utils/central_log.py</p><pre>import json<br>import traceback<br>from config.config import service_name, env_name<br><br>import logging<br>logger = logging.getLogger(__name__)<br><br>def log(level, message, data=None):<br>    entry = {<br>        &quot;level&quot;: level,<br>        &quot;message&quot;: message,<br>        &quot;service&quot;: service_name,<br>        &quot;version&quot;: version,<br>        &quot;env&quot;: env_name<br>    }<br><br>    if data:<br>        entry[&quot;data&quot;] = data<br>  <br>    if level == &quot;error&quot;:<br>        entry[&quot;stack&quot;] = traceback.format_exc()<br><br>    payload = json.dumps(entry)<br>    if level == &quot;error&quot;:<br>        logger.error(payload)<br>    elif level == &quot;warning&quot;:<br>        logger.warning(payload)<br>    else:<br>        logger.info(payload)</pre><p>Let’s implement the main.py entry point module to initialize and load application.</p><pre>from dotenv import load_dotenv<br>from utils.load_secrets import load_secrets<br>import os<br>import uvicorn<br><br># Load environment variables from GCP Secret Manager<br>load_secrets()<br><br># Load environment variables from .env into the environment<br>load_dotenv()<br><br>if __name__ == &quot;__main__&quot;:<br>    if os.getenv(&quot;INSIDE_DOCKER&quot;):<br>        uvicorn.run(&quot;app:app&quot;, host=&quot;0.0.0.0&quot;, port=8000, reload=False)<br>    else:<br>        uvicorn.run(&quot;app:app&quot;, port=8000, reload=True)</pre><p>You could have here conditional logic of loading environment, but I prefer this approach since I can override some settings from local ones, it is extremely helpful for development when you have dozens of secrets.</p><p>If we deploy this service in to Docker the file .env is not going to be added to container, so the container never has secrets.</p><p>You can add your own .env file in to project</p><pre>NAME_ABC=VALUE_XYZ<br>...</pre><p>And escape it from including to git in .gitignore file.</p><pre>__pycache__<br>.DS_Store<br>.idea/<br><br>.coverage<br><br>.env<br>.venv/<br><br>terraform/.terraform.lock.hcl<br>terraform/.terraform</pre><p>Additionally, I added here temporary files for terraform, virtual environment, test coverage and IDEs.</p><p>This is a good start already, but we need to add requirements.txt with required libraries.</p><pre>fastapi<br>pydantic<br>uvicorn<br>aiohttp<br>retry<br>coverage<br>pyyaml<br>google-cloud-secret-manager<br>python-dotenv</pre><p>Here we have a standard set of libraries for the API service application.</p><p>For the simplicity of the project let’s initialize all __init__.py files with empty content. It would tell to python that this folder is module, that is sufficient for this example.</p><p>Now it is time to implement the first interface service_api.py:</p><pre>import json<br><br>from schemas.schema import *<br>from fastapi import Depends, HTTPException, APIRouter<br>from fastapi.security import HTTPBearer<br>from fastapi import Request<br>from fastapi.security.http import HTTPAuthorizationCredentials<br><br>from utils.central_log import log<br><br>router = APIRouter(prefix=&quot;/v1&quot;)<br>security = HTTPBearer()<br><br>@router.get(&quot;/status&quot;)<br>async def status():<br>    log(&quot;info&quot;, &quot;get status&quot;)<br>    return {&quot;status&quot;: &quot;ok&quot;}<br></pre><p>In the file schema/schema.py we should have API payloads, but let’s keep it empty for now</p><pre>from pydantic import BaseModel<br>from typing import Any, List, Dict, Optional</pre><p>Now we are ready to implement application itself in file app.py.</p><pre>from fastapi import FastAPI, Request<br>from fastapi.responses import Response<br>from fastapi.middleware.cors import CORSMiddleware<br><br>import config.config as config<br>from routers import service_api<br><br>app = FastAPI(title=config.service_name, description=f&quot;${config.service_name} Service&quot;, version=config.version)<br><br>origins = [&quot;*&quot;]<br><br>app.add_middleware(<br>    CORSMiddleware,<br>    allow_origins=origins,<br>    allow_credentials=True,<br>    allow_methods=[&quot;*&quot;],<br>    allow_headers=[&quot;*&quot;],<br>)<br><br>@app.get(&quot;/&quot;)<br>async def read_root():<br>    return {&quot;status&quot;: &quot;ok&quot;}<br><br>app.include_router(service_api.router)</pre><p>In this file we are creating FastAPI application that going to serve:</p><ul><li>Read root on /path.</li><li>Service_api router.</li></ul><p>We have to reply status_code=200 on root path, otherwise GKE thinks that container is not healthy and kills it.</p><p>Now the application development is finished, let’s create the docker container.</p><h3>Docker</h3><p>This application it easy dockerize because it is stateless and has zero dependencies on other applications.</p><p>Let’s create the Dockerfile</p><pre>FROM python:3.10.0-slim-bullseye<br><br># Set environment variables<br>ENV PYTHONDONTWRITEBYTECODE=1<br>ENV PYTHONUNBUFFERED=1<br>ENV INSIDE_DOCKER=1<br><br># Create non-root user and group<br>RUN groupadd -r app &amp;&amp; useradd -r -g app -d /app app<br><br># Set working directory and ensure it&#39;s owned by the non-root user<br>WORKDIR /app<br>RUN mkdir -p /app &amp;&amp; chown app:app /app<br><br># Set trusted pip hosts (optional security/perf)<br>ENV PIP_TRUSTED_HOST=&quot;pypi.org pypi.python.org files.pythonhosted.org&quot;<br><br># Copy application code and requirements with correct ownership<br>COPY --chown=app:app . /app<br><br># Upgrade pip and install requirements (as root)<br>RUN pip install --upgrade pip &amp;&amp; \<br>    pip install --no-cache-dir -r /app/requirements.txt<br><br># Optional: pre-run tests as root (if needed)<br># RUN python -m coverage run -m unittest discover tests &amp;&amp; python -m coverage report -m<br><br># Switch to non-root user<br>USER app<br><br># Expose service port<br>EXPOSE 8000<br><br>CMD [&quot;python&quot;, &quot;main.py&quot;]</pre><p>We are using env <em>INSIDE_DOCKER</em> to differentiate run inside and outside the docker container.</p><p>Sometimes it is had to remember all build parameters for the Docker container, therefore let’s create Makefile as a placeholder for future more complex build logic.</p><pre>PROJECT := example-api-service<br>VERSION := $(shell git describe --tags --always --dirty)<br>PLATFORM := $(if $(PLATFORM),$(PLATFORM),linux/arm64)<br><br>all: build<br><br>version:<br> @echo $(VERSION)<br><br>build: version<br> docker build -f Dockerfile --tag $(PROJECT):$(VERSION) --platform $(PLATFORM) .<br><br>run:<br> docker run -it -p 8000:8000 -e SERVICE_ACCOUNT_KEY $(PROJECT):$(VERSION)</pre><p>Now we are finished with application itself, and in the next part we will work on creation of the endpoint for the application.</p><h3>Summary</h3><p>New we should have the application buildable locally and working with local environments .env with potencially load them from GCP Security Manager. This application requires for now only two environment variables:</p><ul><li>SERVICE_ACCOUNT_KEY — encoded GCP credentials in Base64</li><li>ENV_NAME — environment name: local, dev, staging, prod, etc.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2365299e8cc6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ingress controller in k8s cluster on GCP by Terraform. Part 2.]]></title>
            <link>https://medium.com/@karagatan/ingress-controller-in-k8s-cluster-on-gcp-by-terraform-part-2-202fad81dc2d?source=rss-bc74cf7fc2b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/202fad81dc2d</guid>
            <category><![CDATA[gke]]></category>
            <category><![CDATA[gcp]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[ingress]]></category>
            <dc:creator><![CDATA[Alexander @ Karagatan]]></dc:creator>
            <pubDate>Sun, 20 Jul 2025 02:22:03 GMT</pubDate>
            <atom:updated>2025-07-22T06:04:18.975Z</atom:updated>
            <content:encoded><![CDATA[<p>Incoming traffic (ingress) management in GKE by Terraform and CI/CD pipeline.</p><p>In the first <a href="https://medium.com/@karagatan/operating-k8s-cluster-on-gcp-by-terraform-part-1-8356ac33bb86">Part 1</a> we created the source code structure and first GKE cluster. Let’s assume that we want to create a simple web-site in Internet. The primary requirement is not to serve only HTTP traffic, but also to request and issue Let’s Encrypt TLS certificates, create endpoints and serve HTTPS traffic.</p><p>There are two options to organize the ingress in k8s:</p><ol><li>Use Ingress-Controller</li><li>Use new Gateway API</li></ol><p>Believe me or not, I spent a lot of time trying to make things working by using modern Gateway API in GCP/GKE with their own controllers: gke-l7-global-external-managed and gke-l7-regional-external-managed that requires a lot of steps:</p><ul><li>Enable Gateway API in GKE cluster</li><li>Install Gateway API CRDs</li><li>Define the GatewayClass, Gateway, HTTRouter resources</li><li>Use ManagedCertificate</li><li>Use CertMap</li><li>Use LoadBalancer for gateway controller</li><li>Use regional network for regional controller</li><li>Use Cert-Manager since ManagedCertificates are not working</li><li>Too sensitive and limited gateway api controller</li></ul><p>But I was not able to achieve automatic Let’s Encrypt / GCP certificates generation for the GCP Endpoints. Therefore my primary option became to use the legacy open source ingress controller, it just works.</p><p>Another benefit of using the legacy ingress controller —to be independent from the particular k8s cluster implementation.</p><p>From the existing Ingress controllers I selected the most popular one: ingress-nginx. It works very well with cert-manager.</p><h3>Prerequisites</h3><ul><li>Project Name — devops-example-project</li><li>Cluster Name — devops-cluster</li><li>Already created source code project in <a href="https://medium.com/@karagatan/operating-k8s-cluster-on-gcp-by-terraform-part-1-8356ac33bb86">Part 1</a>.</li><li>Already created GKE cluster in <a href="https://medium.com/@karagatan/operating-k8s-cluster-on-gcp-by-terraform-part-1-8356ac33bb86">Part 1</a>.</li></ul><h3>Terraform</h3><p>Let’s create ingress folder in our source code project and terraform files.</p><pre>cd terraform/ingress<br><br>cat &gt; .terraform-version<br>1.12.1</pre><p>The fixed terraform version helps to keep things organized and reproducible.</p><pre>cat &gt; versions.tf<br><br>terraform {<br>  required_providers {<br>    google = {<br>      source  = &quot;hashicorp/google&quot;<br>      version = &quot;~&gt; 6.44&quot;<br>    }<br>    helm = {<br>      source  = &quot;hashicorp/helm&quot;<br>      version = &quot;~&gt; 2.0&quot;<br>    }<br>  }<br>}</pre><p>The most important here is the version of helm plugin.</p><p>Let’s start with terraform backend</p><pre>terraform {<br>  backend &quot;gcs&quot; {<br>    bucket  = &quot;devops-terraform-state&quot; # You must create this bucket first<br>    prefix  = &quot;devops-cluster/ingress&quot;<br>  }<br>}</pre><p>The bucket we already created in <a href="https://medium.com/@karagatan/operating-k8s-cluster-on-gcp-by-terraform-part-1-8356ac33bb86">Part 1</a>. The prefix must be unique and not intercept with any other one, since terraform would keep here state for ingress resources only.</p><p>Let’s add variables that would be used in this config</p><pre>cat &gt; variables.tf<br><br>variable &quot;project_id&quot; {<br>  type    = string<br>  default = &quot;devops-example-project&quot;<br>}<br><br>variable &quot;region&quot; {<br>  type    = string<br>  default = &quot;us-central1&quot;<br>}<br><br>variable &quot;region_with_zone&quot; {<br>  type    = string<br>  default = &quot;us-central1-a&quot;<br>}<br><br>variable &quot;cluster_name&quot; {<br>  type    = string<br>  default = &quot;devops-cluster&quot;<br>}<br><br>variable &quot;email&quot; {<br>  type    = string<br>  default = &quot;your_email@your_host.com&quot;<br>}</pre><p>The email variable is needed for the Let’s Encrypt account. Make sure that it is your working email or group email, you will get notifications from Let’s encrypt about your certificates.</p><p>Let’s define the main.tf file that has a reference to the GKE cluster that we’v created in <a href="https://medium.com/@karagatan/operating-k8s-cluster-on-gcp-by-terraform-part-1-8356ac33bb86">Part 1</a>. For that we are using the data resource that has only reference to the existing cluster. Remember, I specially separated cluster creation to decrease the risks of operations.</p><pre>cat &gt; main.tf<br><br><br># Configure the Google Cloud provider<br>provider &quot;google&quot; {<br>  project = var.project_id<br>  region  = var.region<br>}<br><br># Data source for Google client config<br>data &quot;google_client_config&quot; &quot;default&quot; {}<br><br>data &quot;google_container_cluster&quot; &quot;gke&quot; {<br>  name     = var.cluster_name<br>  location = var.region_with_zone<br>}<br><br># Configure the Kubernetes provider<br>provider &quot;kubernetes&quot; {<br>  host                   = &quot;https://${data.google_container_cluster.gke.endpoint}&quot;<br>  cluster_ca_certificate = base64decode(data.google_container_cluster.gke.master_auth[0].cluster_ca_certificate)<br>  token                  = data.google_client_config.default.access_token<br>}<br><br># Configure the Helm provider<br>provider &quot;helm&quot; {<br>  kubernetes {<br>    host                   = &quot;https://${data.google_container_cluster.gke.endpoint}&quot;<br>    cluster_ca_certificate = base64decode(data.google_container_cluster.gke.master_auth[0].cluster_ca_certificate)<br>    token                  = data.google_client_config.default.access_token<br>  }<br>}</pre><p>Helm provider would be needed to install ingress controller and cert-manager.</p><p>For the external web-site we will need the static IP address.</p><pre>cat &gt; external-ip.tf<br><br># Reserve a global static IP address<br><br>resource &quot;google_compute_address&quot; &quot;external_ip&quot; {<br>  name   = &quot;devops-cluster-dev-static-ip&quot;<br>  region = var.region<br>}</pre><p>This is a regional IP address, since we have only one GKE cluster in one region/zone.</p><p>Let’s define the ingress controller:</p><pre>cat &gt; ingress.tf<br><br>resource &quot;kubernetes_namespace&quot; &quot;ingress_nginx&quot; {<br>  metadata {<br>    name = &quot;ingress-nginx&quot;<br>  }<br><br>  lifecycle {<br>    ignore_changes = [metadata]<br>  }<br>}<br><br># Install NGINX Ingress Controller using Helm<br>resource &quot;helm_release&quot; &quot;ingress_nginx&quot; {<br>  name             = &quot;ingress-nginx&quot;<br>  repository       = &quot;https://kubernetes.github.io/ingress-nginx&quot;<br>  chart            = &quot;ingress-nginx&quot;<br>  version          = &quot;4.12.4&quot;<br>  namespace        = kubernetes_namespace.ingress_nginx.metadata[0].name<br>  create_namespace = false  <br><br>  # This mimics the --upgrade --install behavior<br>  force_update    = false<br>  recreate_pods   = false<br>  cleanup_on_fail = true<br><br>  # Increase timeout for GKE deployment<br>  timeout = 900  # 15 minutes<br>  wait    = true<br><br>  # Configure static external IP (regional)<br>  set {<br>    name  = &quot;controller.service.loadBalancerIP&quot;<br>    value = google_compute_address.external_ip.address<br>  }<br><br>  # Ensure service type is LoadBalancer<br>  set {<br>    name  = &quot;controller.service.type&quot;<br>    value = &quot;LoadBalancer&quot;<br>  }<br><br>  depends_on = [<br>    kubernetes_namespace.ingress_nginx,<br>    google_compute_address.external_ip<br>  ]<br>}</pre><p>We also need cert-manager for this ingress to issue certificates</p><pre>cat &gt; cert-manager.tf<br><br># Create cert-manager namespace<br>resource &quot;kubernetes_namespace&quot; &quot;cert_manager&quot; {<br>  metadata {<br>    name = &quot;cert-manager&quot;<br>  }<br><br>  lifecycle {<br>    ignore_changes = [metadata]<br>  }<br>}<br><br># Install cert-manager using Helm<br>resource &quot;helm_release&quot; &quot;cert_manager&quot; {<br>  name       = &quot;cert-manager&quot;<br>  repository = &quot;https://charts.jetstack.io&quot;<br>  chart      = &quot;cert-manager&quot;<br>  version    = &quot;v1.17.4&quot;<br>  namespace  = kubernetes_namespace.cert_manager.metadata[0].name<br><br>  set {<br>    name  = &quot;installCRDs&quot;<br>    value = &quot;false&quot;<br>  }<br><br>  depends_on = [kubernetes_namespace.cert_manager]<br>}<br><br><br>resource &quot;kubernetes_manifest&quot; &quot;letsencrypt_cluster_issuer&quot; {<br>  manifest = {<br>    apiVersion = &quot;cert-manager.io/v1&quot;<br>    kind       = &quot;ClusterIssuer&quot;<br>    metadata = {<br>      name = &quot;letsencrypt-prod&quot;<br>    }<br>    spec = {<br>      acme = {<br>        email = var.email<br>        server = &quot;https://acme-v02.api.letsencrypt.org/directory&quot;<br>        privateKeySecretRef = {<br>          name = &quot;letsencrypt-prod&quot;<br>        }<br>        solvers = [<br>          {<br>            http01 = {<br>              ingress = {<br>                class = &quot;nginx&quot;<br>              }<br>            }<br>          }<br>        ]<br>      }<br>    }<br>  }<br><br>  depends_on = [helm_release.cert_manager]<br>}</pre><p>For the cert-manager I specially selected option <strong>installCRDs=false</strong> since I would like to install them manually. By doing this we are removing class-level CRDs from managing by Terraform. Otherwise we would have a lot of installation, update, upgrade issues with endless fails in terraform apply.</p><p>Let’s install CRDs manually</p><pre># login to GKE cluster<br>gcloud container clusters get-credentials devops-cluster \<br>  --zone us-central1-a \<br>  --project $PROJECT<br><br>curl -L -o cert-manager-crds.yaml https://github.com/cert-manager/cert-manager/releases/download/v1.17.4/cert-manager.crds.yaml<br><br>kubectl apply -f cert-manager.crds.yaml</pre><p>And finally, let’s define the outputs for the terraform</p><pre>cat &gt; outputs.tf<br><br># Output the external IP address<br>output &quot;nginx_ingress_ip&quot; {<br>  description = &quot;The external IP address of the NGINX Ingress Controller&quot;<br>  value       = google_compute_address.external_ip.address<br>}<br><br># Output the ingress class name<br>output &quot;ingress_class_name&quot; {<br>  description = &quot;The name of the ingress class&quot;<br>  value       = &quot;nginx&quot;<br>}</pre><p>After executing the terraform commands we should get <strong>External IP address</strong> under the <strong>nginx_ingress_ip</strong>.</p><p>But before running the terraform scripts from the console, we need to authenticate by GCP</p><pre>gcloud auth login<br>gcloud config set project devops-example-project</pre><p>and now we are ready to run scripts in the repo</p><pre><br>cd terraform/ingress<br>terraform init<br>terraform plan<br>teraform apply</pre><p>After executing our scripts at the first time we should create in k8s</p><ul><li>namespace <strong>ingress-nginx</strong> with installed ingress controller</li><li>namespace <strong>cert-manager </strong>with installed certificate manager</li><li>static ip address <strong>external_ip</strong></li></ul><p>We can use External IP in DNS manager for any domain name or can create GCP Endpoint and assign External IP address to it. Both options works well.</p><p>You can login to your GKE cluster and verify the installation</p><pre># login to GKE cluster<br>gcloud container clusters get-credentials devops-cluster \<br>  --zone us-central1-a \<br>  --project $PROJECT<br><br>kubectl get namespaces</pre><p>You should have two namespaces created: ingress-nginx, cert-manager.</p><p>Wait 1–2 minutes…</p><p>Your <strong>external_ip</strong> address should match the ingress controller External IP address:</p><pre>kubectl get ingress -n ingress-nginx</pre><h3>CI/CD pipeline</h3><p>As of now we are ready to automate updates by using GitHub Actions.</p><p>In our source code project create the file .github/workflows/deploy-ingress.yaml that will automatically apply terraform changes in the GKE cluster from our <strong>ingress</strong> folder in the main branch.</p><pre>name: Build and Push to GCP<br><br>on:<br>  push:<br>    branches:<br>      - main<br><br>env:<br>  PROJECT_ID: devops-example-project<br>  REGION_WITH_ZONE: us-central1-a<br>  CLUSTER_NAME: devops-cluster<br><br>jobs:<br>  terraform-deploy:<br>    runs-on: ubuntu-latest<br><br>    defaults:<br>      run:<br>        working-directory: terraform/gateway<br><br>    steps:<br>      - name: Checkout source code<br>        uses: actions/checkout@v3<br><br>      - name: Set up Terraform<br>        uses: hashicorp/setup-terraform@v2<br>        with:<br>          terraform_version: 1.12.1<br><br>      - name: Authenticate to Google Cloud<br>        uses: google-github-actions/auth@v2<br>        with:<br>          credentials_json: &#39;${{ secrets.GCP_DEPLOYMENTS_SA_KEY }}&#39;<br><br>      - name: Get GKE Credentials<br>        run: |<br>          gcloud container clusters get-credentials ${{ env.CLUSTER_NAME }} \<br>            --region ${{ env.REGION_WITH_ZONE }} \<br>            --project ${{ env.PROJECT_ID }}<br><br>      - name: Terraform Init<br>        run: terraform init<br><br>      - name: Terraform Plan<br>        run: terraform plan<br><br>      - name: Terraform Apply<br>        run: terraform apply -auto-approve</pre><p>Use your own <em>PROJECT_ID, REGION_WITH_ZONE, CLUSTER_NAME</em> variables.</p><p>This workflow action is using the GitHub secret <em>GCP_DEPLOYMENTS_SA_KEY </em>that contains the GCP Service Account Key in JSON or Base64 formats.</p><p>In the <a href="https://medium.com/@karagatan/operating-k8s-cluster-on-gcp-by-terraform-part-1-8356ac33bb86">Part 1</a> we’v created the service account <strong>devops-gke-nodes</strong>, but it has too many permissions and mostly designed for the GKE cluster creation. Therefore, let’s separate service accounts and create another one for deployments only.</p><p>Let’s name this service account <strong>devops-gke-deployments</strong>. We would have one for managing <strong>nodes</strong> another for <strong>deployments</strong>.</p><pre>gcloud iam service-accounts create devops-gke-deployments \<br>  --project=$PROJECT \<br>  --description=&quot;DevOps GKE Deployments Service Account&quot; \<br>  --display-name=&quot;DevOps Deployments SA&quot;</pre><p>In this account we should add permissions to access Artifact Registry to download docker images that will use later for the application deployments.</p><pre>PROJECT=devops-example-project<br>GKE_DEPLOYMENTS_SA=devops-gke-deployments<br><br>gcloud projects add-iam-policy-binding $PROJECT \<br>  --member=&quot;serviceAccount:$GKE_DEPLOYMENTS_SA@$PROJECT.iam.gserviceaccount.com&quot; \<br>  --role=&quot;roles/artifactregistry.writer&quot;<br><br>gcloud projects add-iam-policy-binding $PROJECT \<br>  --member=&quot;serviceAccount:$GKE_DEPLOYMENTS_SA@$PROJECT.iam.gserviceaccount.com&quot; \<br>  --role=&quot;roles/storage.admin&quot;</pre><p>And for the terraform operations we should add those permissions:</p><pre>gcloud projects add-iam-policy-binding $PROJECT \<br>  --member=&quot;serviceAccount:$GKE_DEPLOYMENTS_SA@$PROJECT.iam.gserviceaccount.com&quot; \<br>  --role=&quot;roles/container.developer&quot;<br><br>gcloud projects add-iam-policy-binding $PROJECT \<br>  --member=&quot;serviceAccount:$GKE_DEPLOYMENTS_SA@$PROJECT.iam.gserviceaccount.com&quot; \<br>  --role=&quot;roles/container.clusterViewer&quot;<br><br>gcloud projects add-iam-policy-binding $PROJECT \<br>  --member=&quot;serviceAccount:$GKE_DEPLOYMENTS_SA@$PROJECT.iam.gserviceaccount.com&quot; \<br>  --role=&quot;roles/storage.admin&quot;<br><br>gcloud projects add-iam-policy-binding $PROJECT \<br>  --member=&quot;serviceAccount:$GKE_DEPLOYMENTS_SA@$PROJECT.iam.gserviceaccount.com&quot; \<br>  --role=&quot;roles/viewer&quot;</pre><p>If you do something outside of those permissions, no worries, you can add more later.</p><p>Let’s also create Artifact Repository for the docker images</p><pre>gcloud artifacts repositories create devops-docker-repo \<br>  --repository-format=docker \<br>  --location=us-central1 \<br>  --description=&quot;Docker repository for DevOps&quot;</pre><p>New we are ready to download deployments SA key from GCP Web Console -&gt; Service Account -&gt; <strong>devops-gke-deployments</strong> and store it in repo or org level:</p><ol><li>GitHub -&gt; Source Code Repo -&gt; Settings -&gt; Secrets and variables -&gt; Actions -&gt; Repository secrets.</li><li>GitHub -&gt; Organization &gt; Settings -&gt; Secret and variables -&gt; Actions -&gt; Organization secrets.</li></ol><p>If you have only one GKE cluster, it is a good choice to store in org level.</p><p>You could have different GKE clusters for example for <strong>dev</strong>, <strong>staging</strong>, <strong>prod</strong>. In this case you will have three workflows that knows what cluster you are updating and use appropriate <em>GCP_DEPLOYMENTS_[ENV]_SA_KEY</em>. My recommendation is to keep association <strong><em>main</em></strong><em> -&gt; prod, </em><strong><em>dev</em></strong><em> -&gt; dev, </em><strong><em>releases/*</em></strong><em> -&gt; staging</em>. Do branch for each release like <strong><em>releases</em></strong><em>/</em><strong><em>yyyy-mm-dd</em></strong>, since you can not add more commits to tags in case of urgent prod bugs.</p><p>Here it is, we do not need manually initialize terraform and apply changes anymore. Every time when we commit to the main branch the GitHub actions automatically runs action that authenticates in GKE cluster with service account <strong>devops-gke-deployments</strong> and applies terraform scripts for ingress folder. This process called automated CI/CD pipeline for DevOps.</p><p>In your source code project</p><pre>git add .<br>git commit -m &quot;my changes&quot;<br>git push</pre><p>It will trigger the workflow and in the GitHub page of the repository in the tab Actions you would have one Action started to deploy the changes to the GKE cluster directly.</p><h3>Summary</h3><p>In this part we created terraform scripts that install ingress controller and cert manager, assign the external static IP address. Automated deployment by CI/CD pipeline through GitHub Actions.</p><p>The next is <a href="https://medium.com/@karagatan/fastapi-simple-rest-application-in-gke-with-terraform-ci-cd-part-3-2365299e8cc6">Part 3</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=202fad81dc2d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating k8s cluster on GCP by Terraform. Part 1.]]></title>
            <link>https://medium.com/@karagatan/operating-k8s-cluster-on-gcp-by-terraform-part-1-8356ac33bb86?source=rss-bc74cf7fc2b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/8356ac33bb86</guid>
            <category><![CDATA[gcp]]></category>
            <category><![CDATA[gke]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Alexander @ Karagatan]]></dc:creator>
            <pubDate>Sun, 20 Jul 2025 00:41:31 GMT</pubDate>
            <atom:updated>2025-07-20T02:49:44.257Z</atom:updated>
            <content:encoded><![CDATA[<p>Using Terraform to create and maintain the GKE Kubernetes Cluster.</p><p>All cloud providers are interested to lock you in to their services and maximize the adoption of Cloud Native. Using services that billing per usage or even per transaction is the dream for cloud companies. Your bill will sky rocket as more users you have. Your application is hard to move and switch between cloud providers. But there is a solution….</p><p>It is the Kubernetes!</p><p>You can have your own universe inside the kubernetes, but how to manage this situation?</p><p>Would it be the case where DevOps costs exceed the whole gain of having it by your own?</p><p>Here we will try to use amazing tool called Terraform where we can place all our infrastructure in code and automate k8s clusters creation in any cloud provider and on prem.</p><p>First of all let’s create GKE Kubernetes cluster in GCP.</p><p>There are two options to do this.</p><p>The first option is go through GCP web interface. You can select the cluster type, name, params, networks and click create. It has a lot of settings and you do not know or can not remember what to use.</p><p>The second option is to use Terraform. Le’t go this route.</p><p>Requirements:</p><ul><li>Already installed Google Cloud SDK in your laptop/desktop. Use the installation <a href="https://cloud.google.com/sdk/docs/install">manual</a>.</li><li>Having ready account in GCP with admin/extended permissions.</li><li>Having credits or billing in GCP account since all those operations are payable.</li><li>Having GCP project, let’s name it <strong>devops-example-project</strong>.</li></ul><p>Majority of commands including the terraform itself needs local login in GCP:</p><pre>gcloud auth login<br>gcloud config set project devops-example-project</pre><p>The project with billing should be already created in GCP web interface, there is no command to create it. If you are a startup do not forget to apply and use GCP credits for free.</p><blockquote>The project names are global and finding the good name is the challenge.</blockquote><h3>Source Code</h3><p>The idea behind the DevOps is to have source code describing infrastructure, not readme/wiki files.</p><p>Let’s create the project structure that we will store in GitHub.</p><p>GitHub selected because it has two major features:</p><ul><li>Action Secrets</li><li>Workflows</li></ul><p>Both of those features we will use later. You are free to choose any other code repository that supports both, but workflow syntax will be provided only for GitHub.</p><pre>terraform<br>    cluster<br>       .terraform-version<br>       backend.tf<br>       main.tf<br>       outputs.tf<br>       variables.tf<br>       terraform.tfvars<br>    ingress<br>       .terraform-version<br>       backend.tf<br>       cert-manager.tf<br>       external-ip.tf<br>       ingress.tf<br>       main.tf<br>       outputs.tf<br>       variables.tf<br>       versions.tf<br>       terraform.tfvars<br>.gitignore<br>README.md</pre><p>Here is the example project that you can structure by any other way. My decision was to separate the cluster creation form other cluster-ops.</p><p>Why do we need a cluster creation folder?</p><ul><li><strong>Portable</strong> — If tomorrow I would like to move it to another project this config preserves all settings and params for the k8s cluster.</li><li><strong>Placeholder</strong> — This example project is simple, but real-world configs are more complex.</li><li><strong>Specific</strong> — The cluster folder is the cloud provider specific, whereas other folders are re-usable between providers.</li></ul><h3>Service Account</h3><p>Cluster creation from Terraform needs certain permissions that you must have in service account.</p><p>Let’s create the service account and assign permissions:</p><pre>PROJECT=devops-example-project<br>GKE_NODES_SA=devops-gke-nodes<br><br>gcloud iam service-accounts create $GKE_NODES_SA \<br>  --project=$PROJECT \<br>  --description=&quot;GKE node pool service account&quot; \<br>  --display-name=&quot;GKE Nodes SA&quot;<br><br>gcloud projects add-iam-policy-binding $PROJECT \<br>  --member=&quot;serviceAccount:$GKE_NODES_SA@$PROJECT.iam.gserviceaccount.com&quot; \<br>  --role=&quot;roles/logging.logWriter&quot;<br><br>gcloud projects add-iam-policy-binding $PROJECT \<br>  --member=&quot;serviceAccount:$GKE_NODES_SA@$PROJECT.iam.gserviceaccount.com&quot; \<br>  --role=&quot;roles/monitoring.metricWriter&quot;<br><br>gcloud projects add-iam-policy-binding $PROJECT \<br>  --member=&quot;serviceAccount:$GKE_NODES_SA@$PROJECT.iam.gserviceaccount.com&quot; \<br>  --role=&quot;roles/monitoring.viewer&quot;<br><br>gcloud projects add-iam-policy-binding $PROJECT \<br>  --member=&quot;serviceAccount:$GKE_NODES_SA@$PROJECT.iam.gserviceaccount.com&quot; \<br>  --role=&quot;roles/container.nodeServiceAgent&quot;<br><br>gcloud projects add-iam-policy-binding $PROJECT \<br>  --member=&quot;serviceAccount:$GKE_NODES_SA@$PROJECT.iam.gserviceaccount.com&quot; \<br>  --role=&quot;roles/artifactregistry.reader&quot;<br><br>gcloud projects add-iam-policy-binding $PROJECT \<br>  --member=&quot;serviceAccount:$GKE_NODES_SA@$PROJECT.iam.gserviceaccount.com&quot; \<br>  --role=&quot;roles/storage.objectViewer&quot;<br><br>gcloud projects add-iam-policy-binding $PROJECT \<br>  --member=&quot;serviceAccount:$GKE_NODES_SA@$PROJECT.iam.gserviceaccount.com&quot; \<br>  --role=&quot;roles/servicemanagement.serviceController&quot;<br><br>gcloud secrets add-iam-policy-binding $PROJECT \<br>  --member=&quot;serviceAccount:$GKE_NODES_SA@$PROJECT.iam.gserviceaccount.com&quot; \<br>  --role=&quot;roles/secretmanager.secretAccessor&quot;</pre><p>This step is specific for GCP. For AWS Azure and on-prems it could be different.</p><h3>Cluster</h3><p>Let’s build the cluster first located in the folder ‘cluster’ in our source code.</p><p>For the terraform version I selected the latest one.</p><pre>cd terraform/cluster<br><br>cat &gt; .terraform-version<br>1.12.1</pre><p>For the backend we use GCP buckets, but you can use any provider specific backend.</p><pre>cat &gt; backend.tf<br><br>terraform {<br>  backend &quot;gcs&quot; {<br>    bucket  = &quot;devops-terraform-state&quot; # You must create this bucket first<br>    prefix  = &quot;devops-cluster-dev/cluster&quot;<br>  }<br>}</pre><p>Let’s create the GCP bucket</p><pre>REGION=us-central1<br><br>gsutil mb -p $PROJECT -c STANDARD -l $REGION gs://devops-terraform-state</pre><p>The terraform state file for the cluster creation would be stored in:</p><pre>gs://devops-terraform-state/devops-cluster/cluster</pre><p>Make it unique for your project and cluster. The common mistake is to copy-past backend.tf and not to change the bucket/path. I selected <strong>devops-cluster </strong>as an unique name of the first k8s cluster in GKE with matching prefix in the bucket.</p><p>For the concurrent ops this bucket stores the lock, you can unlock it by using this command:</p><pre>terraform force-unlock LOCK_ID</pre><p>Let’s define variables that we can configure for our cluster:</p><pre>cat &gt; variables.tf<br><br>variable &quot;project_id&quot; {<br>  type    = string<br>  default = &quot;devops-example-project&quot;<br>}<br><br>variable &quot;region&quot; {<br>  type    = string<br>  default = &quot;us-central1&quot;<br>}<br><br>variable &quot;region_with_zone&quot; {<br>  type    = string<br>  default = &quot;us-central1-a&quot;<br>}<br><br>variable &quot;cluster_name&quot; {<br>  type    = string<br>  default = &quot;devops-cluster&quot;<br>}</pre><p>You also can do this differently by splitting this file in two:</p><pre>cat &gt; variables.tf<br><br>variable &quot;project_id&quot; {<br>  type    = string<br>}<br><br>variable &quot;region&quot; {<br>  type    = string<br>}<br><br>variable &quot;region_with_zone&quot; {<br>  type    = string<br>}<br><br>variable &quot;cluster_name&quot; {<br>  type    = string<br>}<br><br>cat &gt; terraform.tfvars<br><br>project_id = &quot;devops-example-project&quot;<br>region = &quot;us-central1&quot;<br>region_with_zone = &quot;us-central1-a&quot;<br>cluster_name = &quot;devops-cluster&quot;</pre><p>The second approach is better practice to keep configurable variables outside of the terraform code. During deployment you can replace terraform.vars by the custom file specific for the environment: dev, staging, prod. You can also have secret variables like keys and passwords.</p><p>I specially separated region and region_with_zone because Artifact Registry works on the level of region but other deployment resources, like GKE, could be specified with region and zone.</p><p>Now we are ready for the main.tf file that creates the GKE cluster.</p><pre>cat &gt; main.tf<br><br><br>provider &quot;google&quot; {<br>  project = var.project_id<br>  region  = var.region<br>}<br><br>resource &quot;google_container_cluster&quot; &quot;primary&quot; {<br>  name     = var.cluster_name<br>  location = var.region_with_zone<br><br>  remove_default_node_pool = true<br>  initial_node_count       = 1<br><br>  network    = &quot;default&quot;<br>  subnetwork = &quot;default&quot;<br><br>  ip_allocation_policy {}<br><br>  lifecycle {<br>    ignore_changes = [<br>      initial_node_count,<br>      enable_autopilot,<br>      enable_tpu,<br>      enable_intranode_visibility,<br>      datapath_provider,<br>    ]<br>  }<br><br>}<br><br>resource &quot;google_container_node_pool&quot; &quot;primary_nodes&quot; {<br>  name     = &quot;primary-pool&quot;<br>  location = var.region_with_zone<br>  cluster  = google_container_cluster.primary.name<br><br>  autoscaling {<br>    min_node_count = 1<br>    max_node_count = 10<br>  }<br><br>  node_config {<br>    machine_type    = &quot;e2-standard-4&quot;<br>    service_account = &quot;devops-gke-nodes@${var.project_id}.iam.gserviceaccount.com&quot;<br>    oauth_scopes = [<br>      &quot;https://www.googleapis.com/auth/cloud-platform&quot;<br>    ]<br>    labels = {<br>      workload = &quot;primary&quot;<br>    }<br>    tags = [&quot;primary&quot;]<br>  }<br>}</pre><p>We are creating cluster in the specific <em>zone</em> with enabled autoscaling 1..10 with initial 1 node with the size of <em>16gb</em> each and <em>4 vCPUs</em>, and we are ignoring the lifecycle changes and replacing the default pool with the custom one.</p><p>And finally, we need outputs.tf to see the created resources.</p><pre>output &quot;kubernetes_cluster_name&quot; {<br>  value = google_container_cluster.primary.name<br>}<br><br>output &quot;kubernetes_cluster_endpoint&quot; {<br>  value = google_container_cluster.primary.endpoint<br>}</pre><p>After writing all those scripts we can apply changes through terraform:</p><pre>cd terraform/cluster<br>terraform init<br>terraform plan<br>terraform apply</pre><p>It would take several minutes to allocate nodes, install kubernetes and do all registrations.</p><h3>Summary</h3><p>Created source code project in GitHub in branch main that stores the terraform configuration files for the k8s cluster creation in GCP.</p><p>Next is the <a href="https://medium.com/@karagatan/ingress-controller-in-k8s-cluster-on-gcp-by-terraform-part-2-202fad81dc2d">Part 2</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8356ac33bb86" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Replacing Cert-Manager in Kubernetes]]></title>
            <link>https://medium.com/@karagatan/replacing-cert-manager-in-kubernetes-cf1de0e99d6c?source=rss-bc74cf7fc2b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/cf1de0e99d6c</guid>
            <dc:creator><![CDATA[Alexander @ Karagatan]]></dc:creator>
            <pubDate>Wed, 30 Apr 2025 22:21:45 GMT</pubDate>
            <atom:updated>2025-04-30T22:21:45.658Z</atom:updated>
            <content:encoded><![CDATA[<p>How to replace the cert-manager in Kubernetes or update it.</p><p>Long time ago I installed cert-manager in Kubernetes using old approach described in this <a href="https://medium.com/@karagatan/adding-tls-certificates-to-kubernetes-f1176e30fecb">article</a>.</p><p>Having the version of cert-manager 1.15.3 working very well for long time I noticed that there is an issue in cluster caused by changing the API on Cloudflare side.</p><pre>E0430 20:27:39.763346       1 controller.go:162] &quot;re-queuing item due to error processing&quot; err=&lt;<br> while querying the Cloudflare API for DELETE &quot;/zones//dns_records/6a3b8cf5...&quot; <br>   Error: 7003: Could not route to /client/v4/zones/dns_records/6a3b8cf5..., perhaps your object identifier is invalid?<br> &gt; logger=&quot;cert-manager.controller&quot; key=&quot;gateway-infra/...-3230772443-4213816010&quot;<br>user@server:~$     kubectl get deployment -n cert-manager -l app=cert-manager -o jsonpath=&#39;{.spec.template.metadata.labels[&quot;cert-manager.io/version&quot;]}&#39;<br>error: error parsing jsonpath {.spec.template.metadata.labels[&quot;cert-manager.io/version&quot;]}, invalid array index &quot;cert-manager.io/version&quot;</pre><p>By searching the issue in Google I found this PR.</p><p><a href="https://github.com/cert-manager/cert-manager/issues/7540">Cloudflare - DNS Challenge Broken · Issue #7540 · cert-manager/cert-manager</a></p><p>As we can see, the problem was solved in 1.16.4+ version and we need to upgrade.</p><p>Since, I did not install the cert-manager through the helm, I am having the challenge to update it.</p><p>Let’s correct the installation of cert-manager.</p><p>Optional step to download manifests</p><pre>user@kubenode1:~/k8s$ wget https://github.com/cert-manager/cert-manager/releases/download/v1.16.4/cert-manager.crds.yaml<br>user@kubenode1:~/k8s$ wget https://github.com/cert-manager/cert-manager/releases/download/v1.16.4/cert-manager.yaml</pre><p>Using this locally to examine all of them.</p><p>I would also change the original article to use 1.16.4 version of the cert-manager.</p><p>Now let’s upgrade the manifests</p><pre>user@kubenode1:~/k8s$ kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.16.4/cert-manager.yaml</pre><p>Wait some period of time and get status</p><pre>kubectl get all -n cert-manager<br><br>NAME                                           READY   STATUS    RESTARTS   AGE<br>pod/cert-manager-8c879665-wsjwq                1/1     Running   0          25s<br>pod/cert-manager-cainjector-7bfd55d9f7-cpxcp   1/1     Running   0          25s<br>pod/cert-manager-webhook-6c8564598b-dbx4c      1/1     Running   0          24s<br><br>NAME                              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)            AGE<br>service/cert-manager              ClusterIP   10.96.69.221    &lt;none&gt;        9402/TCP           240d<br>service/cert-manager-cainjector   ClusterIP   10.96.176.33    &lt;none&gt;        9402/TCP           25s<br>service/cert-manager-webhook      ClusterIP   10.96.196.235   &lt;none&gt;        443/TCP,9402/TCP   240d<br><br>NAME                                      READY   UP-TO-DATE   AVAILABLE   AGE<br>deployment.apps/cert-manager              1/1     1            1           240d<br>deployment.apps/cert-manager-cainjector   1/1     1            1           240d<br>deployment.apps/cert-manager-webhook      1/1     1            1           240d<br><br>NAME                                                 DESIRED   CURRENT   READY   AGE<br>replicaset.apps/cert-manager-7fbbc65b49              0         0         0       240d<br>replicaset.apps/cert-manager-8c879665                1         1         1       25s<br>replicaset.apps/cert-manager-cainjector-6664fc84f6   0         0         0       240d<br>replicaset.apps/cert-manager-cainjector-7bfd55d9f7   1         1         1       25s<br>replicaset.apps/cert-manager-webhook-59598898fd      0         0         0       240d<br>replicaset.apps/cert-manager-webhook-6c8564598b      1         1         1       24s<br>ashvid@karagatan:~/cert-manager$</pre><p>Checking logs in the cert-manager</p><pre>kubectl logs pod/cert-manager-8c879665-wsjwq -n cert-manager</pre><p>Everything looks good, no more deletion of DNS error.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cf1de0e99d6c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing basic HTTP Server on Golang using Servion Framework]]></title>
            <link>https://medium.com/@karagatan/writing-basic-http-server-on-servion-54e23d7fb2ba?source=rss-bc74cf7fc2b4------2</link>
            <guid isPermaLink="false">https://medium.com/p/54e23d7fb2ba</guid>
            <dc:creator><![CDATA[Alexander @ Karagatan]]></dc:creator>
            <pubDate>Thu, 27 Mar 2025 00:22:12 GMT</pubDate>
            <atom:updated>2025-04-10T22:31:58.413Z</atom:updated>
            <content:encoded><![CDATA[<p>The first basic http server on servion framework in golang</p><p>Today, we will try to build the first server application on <a href="https://github.com/arpabet/servion">servion</a> framework. This framework was build to simplify bootstrapping of the web application, having CLI (Command Line Interface) in place and provide DI (Dependency Injection) capabilities from day one.</p><p>Having those things combined in simple from in golang has a certain value.</p><p>You do not need to start from the scratch and re-invent the wheel, instead, you can use robust and flexible framework to achieve your goals.</p><p>Let’s create the first application.</p><p>We can start from dependencies. In this <a href="https://github.com/arpabet/servion/releases">page</a> select the latest version of the <a href="https://github.com/arpabet/servion">servion</a> and modify the go.mod file.</p><pre>module github.com/yourhandle/yourproject<br><br>go 1.23.0<br><br>toolchain go1.24.1<br><br>require (<br> github.com/gorilla/websocket v1.5.3<br> go.arpabet.com/cligo v0.1.6<br> go.arpabet.com/glue v1.2.4<br> go.arpabet.com/servion v0.1.2<br>)</pre><p>Then run the command to update the dependencies</p><pre>go mod tidy</pre><p>Now, we are ready to create main.go file</p><pre>package main<br><br>import (<br> &quot;go.arpabet.com/cligo&quot;<br> &quot;go.arpabet.com/glue&quot;<br> &quot;go.arpabet.com/servion&quot;<br>)<br><br>func main() {<br><br> properties := glue.MapPropertySource{<br>  &quot;http-server.bind-address&quot;: &quot;0.0.0.0:8000&quot;,<br> }<br><br> beans := []interface{}{<br>  properties,<br>  servion.RunCommand(servion.HttpServerScanner(&quot;http-server&quot;)),<br>  servion.ZapLogFactory(),<br> }<br><br> cligo.Main(cligo.Beans(beans...))<br>}</pre><p>In this simple example we are creating application context containing three beans:</p><ul><li>properties with host and port</li><li>RunCommand instance to provide simple CLI with “run” command.</li><li>Zap Logger</li></ul><p>The Zap Logger is using by default in <a href="https://github.com/arpabet/servion">servion</a> since it is provides JSON logging output, that is good fit for any cloud hosting.</p><p>The HTTP server is located in RunCommand child context, so it would not be created for other commands if we add to the server. This is considered as a good practice.</p><p>Creation of the HTTP server is based on bean name, that is in our case “http-server”. The same bean name we are using in properties.</p><p>Each HTTP server requires as minimum “bind-address” property to correctly start listening the port.</p><p>Let’s build the project</p><pre>go build</pre><p>And run it</p><pre>./basic run<br>2025-03-26T14:39:01.422-0700    INFO    servion@v0.1.1/http_server_factory.go:93        HTTPServerFactory       {&quot;listenAddr&quot;: &quot;0.0.0.0:8000&quot;, &quot;bean&quot;: &quot;http-server&quot;, &quot;handlers&quot;: [], &quot;assets&quot;: [], &quot;options&quot;: {}, &quot;tls&quot;: false}<br>2025-03-26T14:39:01.422-0700    INFO    servion@v0.1.1/utils.go:166     ServionStarted  {&quot;Servers&quot;: 1}<br>2025-03-26T14:39:01.423-0700    INFO    servion@v0.1.1/http_server.go:105       HttpServerServe {&quot;addr&quot;: &quot;0.0.0.0:8000&quot;, &quot;network&quot;: &quot;tcp&quot;, &quot;tls&quot;: false}</pre><p>As we can see our server is started, to stop is press Ctrl-C in terminal</p><pre>^C2025-03-26T14:39:03.468-0700  INFO    servion@v0.1.1/utils.go:193     StopSignal      {&quot;signal&quot;: &quot;interrupt&quot;}<br>2025-03-26T14:39:03.468-0700    INFO    servion@v0.1.1/http_server.go:66        HttpServerShutdown      {&quot;addr&quot;: &quot;0.0.0.0:8000&quot;, &quot;network&quot;: &quot;tcp&quot;}<br>2025-03-26T14:39:03.468-0700    INFO    servion@v0.1.1/run_command.go:69        RunServers      {&quot;restarting&quot;: false}</pre><p>What we learn from the logs, there is a restarting mode of the server, that would close child context and re-create it again to restart all servers without stopping the application. This functionality designed in case if you do not want to kill the pod in Kubernetes cluster, but at the same time need to reload some servers, for example update the secrets or credentials, tls certificates and so on.</p><p>To restart the server user this command.</p><pre>kill -1 &lt;pid&gt;</pre><p>Now we are ready to modify this server a little bit.</p><p>Let’s add additional HTTP Server to the <a href="https://github.com/arpabet/servion">servion</a>.</p><pre>func main() {<br><br> properties := &amp;glue.PropertySource{Map: map[string]interface{}{<br>  &quot;web-server.bind-address&quot;: &quot;0.0.0.0:8000&quot;,<br>  &quot;cdr-server.bind-address&quot;: &quot;0.0.0.0:8001&quot;,<br> }}<br><br> beans := []interface{}{<br>  properties,<br>  servion.RunCommand(servion.HttpServerScanner(&quot;web-server&quot;),<br>   servion.HttpServerScanner(&quot;cdr-server&quot;)),<br>  servion.ZapLogFactory(),<br> }<br><br> cligo.Main(cligo.Beans(beans...))<br>}</pre><p>Now we have two servers: web-server and cdr-server. They both are sharing the same child context, root properties and root logger.</p><p>Let’s build the project</p><pre>go build</pre><p>And run</p><pre> ./two_servers run<br>2025-03-26T17:04:10.144-0700    INFO    servion@v0.1.2/http_server_factory.go:93        HTTPServerFactory       {&quot;listenAddr&quot;: &quot;0.0.0.0:8000&quot;, &quot;bean&quot;: &quot;web-server&quot;, &quot;handlers&quot;: [], &quot;assets&quot;: [], &quot;options&quot;: {}, &quot;tls&quot;: false}<br>2025-03-26T17:04:10.144-0700    INFO    servion@v0.1.2/http_server_factory.go:93        HTTPServerFactory       {&quot;listenAddr&quot;: &quot;0.0.0.0:8001&quot;, &quot;bean&quot;: &quot;cdr-server&quot;, &quot;handlers&quot;: [], &quot;assets&quot;: [], &quot;options&quot;: {}, &quot;tls&quot;: false}<br>2025-03-26T17:04:10.144-0700    INFO    servion@v0.1.2/utils.go:166     ServionStarted  {&quot;Servers&quot;: 2}<br>2025-03-26T17:04:10.144-0700    INFO    servion@v0.1.2/http_server.go:105       HttpServerServe {&quot;addr&quot;: &quot;0.0.0.0:8001&quot;, &quot;network&quot;: &quot;tcp&quot;, &quot;tls&quot;: false}<br>2025-03-26T17:04:10.145-0700    INFO    servion@v0.1.2/http_server.go:105       HttpServerServe {&quot;addr&quot;: &quot;0.0.0.0:8000&quot;, &quot;network&quot;: &quot;tcp&quot;, &quot;tls&quot;: false}</pre><blockquote>Now we see that both servers are started and also demonstrated that servion can have multiple servers in one context.</blockquote><h3>Web-sockets</h3><p>Additionally, the servion framework supports web-sockets indirectly. It provides the flexible interface of initialization of http server, that look up only the HttpHandler. Inside HttpHandler the websocket could be initialized.</p><p>Let’s create the structure for the handler</p><pre>type implWebsocketHandler struct {<br> upgrader *websocket.Upgrader<br>}</pre><p>Inside this structure we have additional private field “upgrader”.</p><p>This field is using to upgrade the connection to the web-socket inside the ServerHTTP method.</p><p>Let’s create the constructor</p><pre>func WebsocketHander() servion.HttpHandler {<br> return &amp;implWebsocketHandler{<br>  upgrader: &amp;websocket.Upgrader{<br>   CheckOrigin: func(r *http.Request) bool {<br>    return true // allow all origins for now<br>   },<br>  },<br> }<br>}</pre><p>In the constructor we need to initialize the “upgrader” and provide the “CheckOrigin” function. This is needed because web-sockets by default could be used from any page. Literally, web-sockets are ideal technology to distribute some functionality by SaaS.</p><p>And finally, we need to implement the method for websocket handler</p><pre>func (t *implWebsocketHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {<br> conn, err := t.upgrader.Upgrade(writer, request, nil)<br> if err != nil {<br>  log.Printf(&quot;[WebSocket] Error upgrading connection: %v&quot;, err)<br>  return<br> }<br> defer conn.Close()<br> log.Println(&quot;Client connected&quot;)<br><br> for {<br>  // Read message from client<br>  _, msg, err := conn.ReadMessage()<br>  if err != nil {<br>   log.Println(&quot;Read error:&quot;, err)<br>   break<br>  }<br><br>  log.Println(&quot;Received:&quot;, string(msg))<br><br>  // Echo message back to client<br>  err = conn.WriteMessage(websocket.TextMessage, msg)<br>  if err != nil {<br>   fmt.Println(&quot;Write error:&quot;, err)<br>   break<br>  }<br> }<br> log.Println(&quot;Client disconnected&quot;)<br>}</pre><p>Please, so not forget to close the connection by “defer conn.Close()”.</p><p>Additionally, we need to provide the Pattern for the handler:</p><pre>func (t *implWebsocketHandler) Pattern() string {<br> return &quot;/v1/examples/websocket&quot;<br>}</pre><p>Since we are building our application by components, we can not have dedicated code for the mux, we would define pattern in each component separately. In this case this code became reusable, we can package it separately and ship to another applications.</p><p>And in order this WebsocketHandler to be used we need to modify our main.go</p><pre>func main() {<br><br> properties := glue.MapPropertySource{<br>  &quot;http-server.bind-address&quot;: &quot;0.0.0.0:8000&quot;,<br>  &quot;http-server.options&quot;:      &quot;handlers&quot;,<br> }<br><br> beans := []interface{}{<br>  properties,<br>  servion.RunCommand(servion.HttpServerScanner(&quot;http-server&quot;), WebsocketHander()),<br>  servion.ZapLogFactory(),<br> }<br><br> cligo.Main(cligo.Beans(beans...))<br>}</pre><p>Additional setting “&lt;bean_name&gt;.options” added to the property. This option is responsible for the enabling certain options in the HttpServerFactory.</p><p>Options:</p><ul><li>“handlers” — it would lookup in context classes HttpHandler and combine them in “mux”</li><li>“assets” — it would lookup in context resources having name with suffix “asset” and use their http.FileSystem to serve static content</li><li>“tls” — it would lookup in context *tls.Config instance and use it for TLS connection</li></ul><p>This framework was created to provide the simple way of building micro-services in Kubernetes and Docker, having minimum functionality only needed for this purpose. Support popular protocols and being lightweight.</p><p>Servion examples: <a href="https://github.com/arpabet/servion-examples">https://github.com/arpabet/servion-examples</a></p><p>Servion Vue Vite example: <a href="https://github.com/arpabet/servion-vue-example">https://github.com/arpabet/servion-vue-example</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=54e23d7fb2ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>